· Define new terms
· Describe the differences between value types and data types
· Explain boxing and how if aids object-oriented design
· Learn about complex data types like classes and structures
· Understand how to select the correct data type for different applications.
Introduction
Computer programs store and use a wide variety of information. Different types of information are stored and used in different ways. The object, being the basic building block of object-oriented programming is an important tool for storing information. From the outside, an object appears to be a unit and it often treated as a complete unit. Objects may hold several individual pieces of information.
C# stores information as either a value type or as a reference type. Value types include the C# predefined numeric types that hold various types of numbers. Value types also include some user-defined types. Reference types include objects that come standard with C# and other, user created objects that are developed using C#. All of these types have their own purposes with limits and features that define how they are used.
Predefined Types
There are both reference and value types that are predefined in the C# language. The predefined reference types are string and object. We will discuss these in detail later. The predefined value types are more familiar to most people and will be discussed first.
The predefined numeric types store and manipulate real and whole numbers. Whole numbers or integers do not have a fractional part. They are ordinal numbers, which means that there are a fixed number of numbers between any set of two numbers. Real numbers, as you might expect, do have a fractional part. This part is shown using numbers to the right of a decimal point. There may be an infinite set of numbers between any two real numbers.
C# has a powerful set of predefined numeric data types. Figure 4-x Numeric Type Descriptions lists these types and their attributes.
|
C# Type |
Size |
Description |
|
byte |
1 |
An unsigned byte may hold a positive number in the range of 0 to 255. Byte variables are used to hold small integers where space is important. |
|
sbyte |
1 |
A signed byte may hold a number from –128 up to 127. The bit used to hold the sign (negative or positive) takes up one of the room in the byte that would be used for larger positive numbers. |
|
short |
2 |
A short holds a small signed (may be positive or negative) integer value between –32,768 up to 32,767. |
|
ushort |
2 |
An unsigned short holds a positive number between 0 and 65,536. |
|
int |
4 |
An int holds an integer value between -2,147,483,648 and 2,147,483,647. |
|
uint |
4 |
An unsigned integer holds a value between 0 and 4,294,967,296. |
|
long |
8 |
A long holds integer values that may range between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807. |
|
ulong |
8 |
An unsigned long value may range from 0 to 18,446,744,073,709,551,616. |
|
float |
4 |
A float is a single precision floating point or real number. Real numbers have a fractional part to the right of a decimal point. Float values range between -3.402823-E38 and 3.402823-E38. |
|
double |
8 |
Doubles are double-precision floating-point numbers and use twice as much room as floats but they can also hold a much wider range of numbers. Double values range from -1.79769313486232-E308 and 1.79769313486232-E308. |
|
decimal |
8 |
Decimal values are treated specially and are frequently used to handle money values. Decimal values are required when it is particularly important to avoid rounding errors to the right of the decimal point. Decimal values support up to four digits to the right of the decimal separator and fifteen digits to the left of the decimal separator. Decimal values range from -922,337,203,685,477.5808 to 922,337,203,685,477.5807. |
Figure 4-x Numeric Type Descriptions
Two other value types deal with characters and Boolean values. The char type holds a single character as a Unicode value. Unicode is an international standard that defines numeric codes for characters in many languages. The bool type holds a value that may represent true or false. Boolean values will be discussed in more detail in chapter 5.
All variables must be declared before they can be used in a C# program. They must also have a value assigned to them before they are used.
C# value types can be used with either variables or literals. Variables are named storage locations for information that can be changed. Literals are values that are entered into the code itself. Literals can only be changed by changing the program code. The following code declares a number of variables.
int myAge;
double aveAge;
double hourlyRate = 6.75;
char cTmp = 'a';
bool bTmp = true;
The variable hourlyRate is set to a value of 6.75. The number 6.75 and character ‘a’ are examples of literal values. The char variable can only hold one character at a time. Single character literals must be enclosed in single quote marks (‘). A bool variable may be set to either true or false.
Numeric literals are assumed to be of type int if there is no decimal point used. If a decimal point is used in a literal then it is assumed to be of type double. This may cause compile errors if literals are used with some assignments. C# has a number of codes that can be used with numeric literals to tell the compiler the type of literal. The following declaration declares and assigns a value to a variable of type decimal. The letter “M” after the first number marks this literal as a type decimal. The ‘F’ after the second number marks it as a float.
decimal Dime =.10M;
float dNumber = 12345F;
The letter ‘L’ can be used to indicate a long integer. A ‘U’ indicates that a literal is an unsigned value. These letters can be combined; “UL” indicates an unsigned long for example. These letters may be used in uppercase or lower case. The lowercase “L” is easily mistaken for the number one so should generally be avoided.
Constants are named values that cannot be changed by the program. They are declared a lot like a variable but with two important differences. The reserved word const must appear before the constant’s type is named. The following code declares a constant called legalAge and sets its value to 18.
const int legalAge = 18;
The C# compiler will not allow a constant to be changed after it has been declared so it must always have a value assigned when it is declared. Constants are valuable because they allow a program to be changed or enhanced more easily. Consider a program that bases many of its actions on whether or not a person is of legal age. It may be that the legal age for the activity involved is 18 when the program is written. The number 18 may figure in many calculations and comparisons throughout the program. What happens if the legal age is one day changed to 21 or 16?
The program will have to be changed in many places. Each place is an opportunity to make a mistake or a change that may be missed. The program will require many checks and much testing before it can be used with confidence. If the legal age is declared as a named constant changing the program is much easier. Everyplace where the legal age is needed for a comparison or a calculation the constant legalAge will be used. If the legal age changes the programmer only needs to change the declaration of the constant and recompile the program. In this case, the program will work as correctly as the unchanged program as long as the constant was used consistently.
A variable or constant name:
Names that do not follow these rules will result in an error at compile time. Names are case-sensitive. That means the uppercase, or capital, letters are not considered the same as lowercase letters. The following variable declarations all define different and unique variables.
int myAge;
int MyAge;
int MyagE;
int myAGE;
It is generally not a good idea to have variables that differ only by the case of the letters. There are some cases where it does make sense and we will discuss them later. The compiler will give an error message if a variable is used that has not been defined. It is good practice to double check the spelling, including the case of the letters, of a variable that causes this error. It may be that the variable was defined but that the spelling or case that was used in the definition is different from what is used in the code.
Names that are too long can cause a number of problems. One is that it takes more typing to enter them and that introduces potential for error. More importunately is that when names get too long and start off very similar there is much room to confuse different variables. Names should be different enough from each other to avoid confusion. It is also a good idea that names be different by more then one character unless the names are very short.
Exercise 4-x:
Look at the following list of possible variables. For each example, indicate if the word is acceptable as a variable name using C# rules. If a word is not acceptable, indicate what rule it violates.
1. mnu123 ____________________________________________________
2. class ____________________________________________________
3. 1stPlace ____________________________________________________
4. one+ ____________________________________________________
5. SaMpLe ____________________________________________________
6. knew number ____________________________________________________
7. ONE ____________________________________________________
8. jOhN ____________________________________________________
9. Bad _______________________________________________
10. one&two _______________________________________________
Assignment statements are the simplest of all statements. Assignment statements are used to copy a value. The assignment statement uses the equal sign ( = ) as its operator. In this case, the equal sign is called the assignment operator. An assignment statement copies in only one direction. The value on the right hand side of the assignment operator is copied into the location named on the left hand side of the operator.
The value on the right hand side of the operator may be a variable, a constant, a literal or a formula that includes several types of values. The left hand of the assignment must be some named location that can be changed. Literals, constants and formulas cannot store a new value so they cannot be on the left hand of an assignment. If you think about this, it makes sense that some values cannot be changed. Imagine what would happen to the mathematical calculations on your computer if you changed the value of the number 1 to 5!
Formulas in a computer program follow the mathematical order of operations very closely. As a quick review, any expression inside parentheses is evaluated first. Multiplication, division and the modulus operation are next in priority. If there are more then one operation in this category the operation on the left is evaluated before one on the right. After these operations are addition and subtraction. These operations are also processed from left to right. There are more operators in C# and you will learn where they fit in the order of precedence as they are presented. Because not all the operators you are used to using in mathematics are found on the keyboard (there is no ¸ or for example) or would be confusing (is it a letter X or a multiply?) there are a set of symbols that C# uses for these operations. Table 4-x Mathematics Operators shows some of these symbols.
|
Symbol |
Operation |
|
++ |
Increment |
|
-- |
Decrement |
|
* |
Multiplication |
|
/ |
Division |
|
% |
Modulus |
|
+ |
Addition |
|
- |
Subtraction |
Table 4-x Mathematics Operators
The modulus operator is similar to the division operator except that it returns the remained of a division. For example 5 % 3 is 2. Five divided by three is one with a remainder of two. That remanded is what modulus returns. All numeric types have predefined modulus operators.
The increment and decrement operators are a little different from other operators. They operate using only one variable. The increment operator adds one to a variable. The decrement operator subtracts one. The following code demonstrates these operators.
cnt++; // Increases the value of cnt by one
cntDown--; // Subtracts one for the value of cntDown
other mathematical operators can be used with the equal sign to change the value of a variable by a single value. For example, to increase a value by two you could do the following:
cnt += 2;
The following code divides a value in half.
cost /=2;
The following code multiplies a value by a value in another variable.
price *= margin;
In all these examples, a new value is calculated and the result is saved in the original variable. The three examples above perform the same operations as the following three assignments.
cnt = cnt + 2;
cost = cost / 2;
price = price * margin;
Assignment statements are used to get and set property values in classes. The properties of Winforms objects such as text boxes, labels, command buttons and forms can be set and read using the same assignment statement that processes values in variables.
C# supplies a Math class that provides a number of useful mathematical methods. These methods can be used in the formulas in assignment statements. The following example code calculates the hypotenuse of a right triangle using the Pythagorean theorem. The Sqrt method calculates a square root while the Pow method raises a number to the power specified.
C = Math.Sqrt( Math.Pow(A,2) + Math.Pow(B,2));
The following table lists and describes a sample of the Math methods.
|
Method |
Description |
|
Abs |
Absolute value of a number |
|
Max |
Returns the higher of two numbers |
|
Min |
Returns the lower of two numbers |
|
Pow |
Returns a number raised to a specified power |
|
Round |
Returns a number rounded off to the nearest whole number |
|
Sqrt |
Returns the square root of a number |
|
Sin, Cos, Tan |
Sine, Cosine and Tangent of an angle in radians |
The value Math.PI is also defined as Π, the ratio of the circumference of a circle to its diameter.
A struct type is a value type that can contain constructors, constants, fields, methods, properties, indexers, operators, and nested types. Structs are used to create values that hold more then one piece of information as parts of an identifiable whole. The struct type is suitable for representing lightweight objects such as Point, Rectangle, and Color.
In its simplest form, a struct definition consists of the reserved word struct followed by the name of the structure and then the body of the structure. The body is enclosed in a set of braces. The following code shows the definition of a struct used to hold a point value.
public struct Point
{
public int x, y;
}
Public indicates that this struct is available throughout to other classes. This type can now be used to declare variables of type Point. First though we can make this struct easier to use by adding a constructor. A constructor is used to set the initial values of the fields in the struct. If there is no constructor then each field will have to be initialized by a separate assignment statement before the variable can be safely used. The following code shows a more complete struct with a constructor. The code with it shows how the variables myPoint and yourPoint are declared, initialized and used.
// struct declaration and initialization
using System;
public struct Point
{
public int x, y;
public Point(int p1, int p2)
{
x = p1;
y = p2;
}
}
class MainClass
{
public static void Main()
{
// Initialize:
Point myPoint = new Point();
Point yourPoint = new Point(20,20);
// Display results:
Console.Write("My Point:\t");
Console.WriteLine("x = {0}, y = {1}", myPoint.x, myPoint.y);
Console.Write("Your Point:\t");
Console.WriteLine("x = {0}, y = {1}", yourPoint.x, yourPoint.y);
}
}
Notice that myPoint is declared with no parameters. The reserved word new is used to allocate space for the new variable of type Point. C# creates a default constructor that will set the values of x and y in the struct to zero automatically. The x and y values of yourPoint are set by the constructor that has been programmed to accept parameters. A struct cannot have a constructor that does not require parameters. This program will display the following result:
My Point: x = 0, y = 0
Your Point: x = 20, y = 20
You can declare a variable using a struct without using new. If you do not use new, the constructor is not called and your program cannot use the variable until its fields have been set to specific values.
The individual fields in a struct are addressed using the dot notation. The name of the struct is followed by a dot or point that separates it from the name of a member of the struct. In the code above, you will see yourPoint.x and yourPoint.y. These directly address the x and y fields in the struct that has the name yourPoint.
Although it is possible to represent points and other items as classes, a struct is more efficient in some cases. For example, if you declare an array of 1000 Point objects, you will allocate additional memory for referencing each object. In this case, the struct is less expensive of memory. Structs are useful when you have an item that is used often and is mostly or all data.
Value types are stored in-line in the program space or in the stack. That means that specific areas in memory are reserved for value types by the compiler. Reference types, such as classes, are stored in the heap. The heap is reserved for the memory allocation needs of the program that develop while it is running. The heap is an area apart from the program code and the stack, which are areas of memory predefined by the compiler. Objects are reference types.
C# implements objects as classes. A class is a type of object. When a program declares a variable as a class type that variable is an object. Think about the concept or definition of a table being the class. An actual physical table is an object of the table class.
The object class is the base class for all other classes. The string type is a predefined reference type, based on the object class, which is used to handle strings of characters. The power of object-oriented program is creating your own classes to solve your own problems
A class begins with the class statement that defines the name of the class. Class names should be descriptive of the class. The definition of the class takes place inside a block marked by a set of braces. Classes will consist of data and methods that act on that data. Some of these methods will place values in the data components while others will retrieve that data. Still other methods will perform more general actions that may use data from inside the class and data that is passed to the method from other methods.
The data in the class are generally the first items to be defined in the class. Variables are declared inside the class as either public or private. Public variables may be directly accessed from outside the class. Private variables may only be accessed by using methods that are part of the class itself. Public variables violate some of the basic rules of data encapsulation that are important in object-oriented programming so are to be avoided as much as possible.
A useful sample class is a dice class. A dice class allows a programmer to simulate dice for games and for random experiments. This class will use the Random class, included with C#, that returns psuedo random numbers. While not completely random, these numbers will be random enough for our purposes.
This dice class will include two private important pieces of data that will be set and returned by the dice class. One value will be the number of sides and the second will be the face value of the die. We will also use a variable of type Random to set the face value to different values.
You will also need a method to return the value on the die. It will also be useful to have a method that changes the value of the die. The sample below creates a simple dice class.
public class Dice
{
private int sides = 6;
private int myFace;
private Random rNum= new Random();
public int getFace()
{
return myFace;
}
public int Roll()
{
myFace = rNum.Next(sides)+1;
return myFace;
}
}
The name of the class is Dice and that is declared in the first line. The three variables are declared as private to prevent outside code from changing them. The number of sides is set to six because that is a standard value for a die in most games. The other values will be set by the system. The value of myFace will be set to zero. We do not know, nor do we need to know at this time, what values are set inside Face. All we need to know is that it will give us a random value when asked.
The public method called getFace returns the current value of myFace. It is not very useful to call this function unless myFace has been set to a random value. You will want to call the next method, Roll, before calling getFace.
Roll has two closely related functions. Roll first calls the Next method of the Random object and copies a new random value into myFace. Roll then returns the new value of myFace. You will notice that this code adds one to the value returned by the Next method. That is because the Next method returns the next random number that is between zero and one number less then the number passed as a parameter. Dice are generally numbered starting at one so this addition ensures that a number in the range of one to six is returned.
Roll could have just changed the value of myFace and not returned anything. This would have been a method of type void and not type int. Void methods do not return any value and return statements are not permitted in them. If we had done this then the programmer would have to use both Roll, to change the value, and get Face to read the value. This Roll method is more efficient if every roll is going to be read. If your program were going to roll the dice a number of times and only check the last value then the two-step process would be slightly more efficient.
It is time to see how our dice class works. The first step is to open Visual Studio and open a new C# Console application.
Add some code in the Main method to declare a variable of type Dice and a Console.WriteLine statement to roll and display the value of the die. You code may look something like the following:
public static int Main(string[] args)
{
Dice myDie = new Dice();
Console.WriteLine("Die value: {0}",myDie.Roll().ToString());
}
return 0;
}
The next step is to add the Dice class. From the Project menu on the IDE, select Add Class and add a C# class named Dice. Enter the sample code for the Dice class used earlier.
Build and run your program. The result should be a random number between one and six on the screen.
Some methods and data members belong to the class and not to a specific object. These methods and data members are declared as static. A value that is used by the class would be static. For example, a static variable can be used to count the number of objects of a class. Or all objects in a class might use the same value for calculating some value, such as legal age for a student class.
The methods in a class that are used in the Main method are often declared as static so that they can be used without declaring an object of the class type.
One or more constructors are the next items to define. A constructor is a method that sets the initial values for the items that make up a class. If there is no constructor declared then the system will set all the values to zero, if numeric, or null if a class. Assignments that are made as part of a variable declaration will also be set. Creating a constructor allows a programmer to select non-default values for the data in a class.
A constructor has the same name as the class. It must have at least one parameter but may have many parameters. Constructors are methods without a type. The following code shows a simple constructor.
public Dice(int noSides)
{
if (noSides == 0)
sides = 6;
else
sides = noSides;
myFace = Roll();
}
This constructor sets values for both the number of sides and the face value of the die even though only one parameter is passed. If the value of the parameter is zero or less, the constructor sets the number of sides to six. It would not make sense to have a die with no sides. This error checking helps to prevent problems with the program later. Another option for handling bad data would be to throw an exception. This option will be discussed in chapter 7.
A constructor with no parameters is called a default constructor. Default constructors are used to set the fields in a class to specific, non-zero values. Because no parameters are passed, these constructors are used to set the most commonly expected values for the class. Constructors with one or more constructors are used for less common values that are different from the default.
Different constructors, all with the same name, can be written as long as either the number or types of parameters are changed. This is called overloading and will be explained shortly.
Properties are a feature of C# that allows programmers to set and retrieve information in class variables in a natural manner. It is inconvenient if a programmer must memorize different methods to change or retrieve information. In the dice class, a programmer must know to use the method getFace. The value returned by the method can be used directly for simple purposes. For other purposes to some complicated steps might be required. This is especially true if we want to make changes to the value based on its current value.
For example, if we have a class called student with an age field and wanted to add a year to the student’s age we might have to write code something like the following.
myStudent.setAge(myStudent.getAge() + 1);
It would be much more natural to write code such as the following.
myStudent.Age++;
The second option could be done with a public variable in the class but that would create other problems. Obviously, it violates the concept of encapsulation in general. More specifically, it means that there is no error checking involved in changing the data. This can create logic errors that are hard to find and fix.
The correct solution is to create a property. A property method uses get and set sections to return and assign a value to a variable in the class. Property methods are public methods of the same type as the variable they manipulate. The name of the property is often very similar to the name of the variable. Some programmers use all lowercase letters to name the variable and an initial capital letter for the property name. it is important that you be careful and consistent if you use this kind of naming convention.
Property methods do not have to implement both a get and a set section. A property that only has a get section is called a read-only property. It may be read by a client but may not be changed using an assignment statement in the client code. Write-only properties can also be created though they are less often useful.
The following code demonstrates a read-only property named Face.
Public int Face
{
get
{
return myFace;
}
}
After the Face property is declared as a public integer method, a get block is created. This block returns the integer value in myFace, which is part of the private data of the class.
A property that allows both read and write would also have a set block. The following code shows a property that is both a read/write property.
public int Cnt
{
set
{
if (value >= 0 && value <= max)
{
cnt = value;
}
else
cnt = min;
}
get
{
return cnt;
}
}
The get property simply returns a stored value. The set clause does range checking to make sure the value passed is valid. Value, in this case, is a pre-defined word in C#. It automatically represents the value that is being passed to the property.
Other code would use this feature by using the same dot notation that is used with a struct. The name of the variable is connected to the property name by a dot (.). The property name used is the name of the method that has been created with a get or set clause. The name of the private variable in the class is not used. In fact, a property could be a combination of information from several private variables or a value that is calculated inside the class.
Overloading
The compiler will select the correct method to use based on the parameters that are used to call the method. For example, if the statement that is used to allocate a new instance of the class does not have any parameters the compiler will select the default constructor. If the allocation uses a single integer parameter, the compiler will use the constructor that is defined with a single integer parameter.
If no method is found that matches the parameters used the compiler will display an error that explains that there is no method with a matching parameter list. This message will also describe the parameters that are being used. This message helps the program understand why the parameter lists do not match.
A number of C# operators may also be overloaded. Both unary operators, operators such as ++ and – that operate on a single value, and binary operators, such as +, *, /, and – that operate using two values, may be overloaded.
Operators are overloaded using the operator key word. The following code overloads the ++ operator for a class called Count.
public static Count operator++(Count lhs)
{
lhs.Incr();
return lhs;
}
This method is a public, static method that returns a value of type Count. Overloaded operator methods are static because they belong to the class. The operator being overloaded, ++, follows the operator key word. The parameter is the value used with this operator in the client code. This value is referred to as lhs inside the method. It is common to call the values used with an operator lhs, for left hand side, and rhs, for right hand side, in operator overloading functions.
Because lhs is of the Count class, this code uses a member function of that class to increment its value. The now modified value is returned. Since the Incr method has changed the value of the variable, you may wonder why any value needs to be returned at all. The answer lies in how this function is used. If this called is called using the following statement it does not matter if a value is returned because the needed change has been made.
myCnt++;
If this operator is used as part of a more involved statement, such as the one below, it will matter.
Console.WriteLine(“ New count {0}”, myCnt++);
If no value is returned by the method then there will not be a value returned to display. The compiler will not allow this to happen. The unary operators must return a value of the class type.
Binary operators do not have to return a value of the class type. The following code shows an overloading of the plus operator. This example returns an integer value.
public static int operator +(Count lhs, Count rhs)
{
int rtn = lhs.cnt + lhs.cnt;
return rtn;
}
Both lhs and rhs are objects of the Count class. Because this method is part of the Count class, this method may access the private data members of the two objects. This direct access of private data must be done very carefully. In this case , the private data is not being changed so this access is not likely to cause problems. Modification to private data, in an input value or a return value, should be done using class methods.
The variable myAge is a value type but the Console.WriteLine method takes an object as its second parameter. The value in myAge will be boxed and the newly created object will be passed to the WriteLine function. The WriteLine function will use the ToString function of the object to convert the value into a string so that is can be written to the console.
Data is also boxed when a value type is copied into an object type. For example, the following code copies an integer into an object.
When one object is copied to another, the target object holds the same reference as the source object. In this case, there is not source object so one is created by boxing. The object o refers to the boxed object in the heap. Boxing also marks the object with the type of the value so that the system knows what type is in the box.
We can convert this object back into a value type by unboxing it. Unboxing requires that the program specify the type of the value in the object. This is called casting. When a value is cast back from an object, the casting must match the type marked in the object. If a decimal number was boxed in the object, attempting to cast it into an integer will throw an exception. Throwing an exception means that your program will have an error that may cause your program to fail. You will read more about exceptions and how to handle them in chapter 7, Error Handling.
C# supports a number of predefined types for processing numbers, characters and Boolean values. This predefined types are referred to as value types because their values are directly stored in the system stack. The process of boxing and unboxing allows these predefined types to be handled as true objects.
Mathematical formulas are implemented in assignment statements. Assignment statements copy the value on the right hand of the equals sign (assignment operator) into the location on the left hand of the formula. The destination must be a variable or property that supports the type of the result.
Structs are complex value types that are designed by programmers. Structs combine several values of similar or different types into one structure that can be processes as a unit.
Classes are reference types that are stored in the system heap. An object is a specific instance of a class in use. Classes include data and methods that process that data. Private data and methods may be used only by the class itself. Public data and methods may be accessed by a client program or method. Public data violates the principal of data hiding so public method should be used to read and set values in class data.
Properties allow a program to get and set values inside a class in a natural manner. A property method uses a get block to return a value. The set block stores a value in class data. Set blocks generally include code to verify the data being saved to protect the integrity of the class.
Methods and operators may be overloaded. Overloading allows the same operator or method to perform differently depending on the information that is given to it. Constructors are commonly overloaded to allow maximum flexibility when creating new objects of a class. Operator overloading allows commonly understood operators to be used with user-written classes.
Boxing is the process of creating a class for a value type so that it can be used as a true object. Unboxing checks the type of value stored in a box and copies that value into a value type.
Assignment statement An assignment statement copies information into a variable.
Boolean Boolean values are values that may be either true or false.
Boxing Boxing is the process of allocating room to store a value on the heap and creating a class to access this data by reference.
Casting Casting is the conversion of a value from one type into another.
Constant A constant is an unchanging value. Constants with names are declared similar to variables. Unnamed constants are called literals.
Constructor A constructor is a method that sets the initial values for the data in a class when is it first allocated.
Default constructor A default constructor initializes the fields in a class using standard values. Default constructors do not use parameters.
Heap The heap is memory that is set aside for use of the program for storage needs that develop while the program is running. Reference types point to area in the heap where their values are stored.
Increment Incrementing a number means to add the number one to its value. In other words, if a variable has a two in it to start, after being incremented it will be a three.
Literal A literal is an unnamed constant value. Literal values do not change while a program is running.
Overloading Two or more methods with the same name and different numbers or types of parameters are overloaded methods. Operators may also be overloaded and behave differently based on the type of parameters.
Predefined Numeric Types Predefined number types are types that are used to store and manipulate real and whole numbers. These value types are built into the C# language.
Property A property uses get and set code to allow client software to use data in a class as if it were public data but with the protection of using class methods.
Reference type Reference types are types that are defined using the class statement. A reference variable holds a reference to the location of the class in the heap.
Static Static methods and data members belong to the class and not to a specific object of the class type.
Throw an exception A program throws an exception when an error takes place in the program. Exceptions may be handled by a method called an exception handler.
Unboxing Unboxing gets the value in an object and copies into a variable of value type.
Value Type A value type variable holds an actual value. Value type information is stored in-line in the program space.
Unicode Unicode is an international standard that defines number codes for characters in many languages.
Variable A variable is a piece of information, with a name, that can be changed by the program.
Projects