C# Tutorial - Dissecting Our Second Application – TypesIn our application, we use a string to hold some data. Data in C# is stored in one of two types; intrinsic (built-in) types offered by the language, and user-defined types that are defined by the programmer. In addition to intrinsic types and user-defined types, C# differentiates between value types and reference types. Value TypesValue types hold their value on the stack, like variables in C++, unless they are embedded within a reference type. Value types include all of the standard predefined integral data types as well as structures and enumerations. All of the simple types, those integral to the C# language, are aliases of the .NET Framework System types. For example you can declare an integer variable by using either of the following declarations: int x = 123; System.Int32 x = 123; Intrinsic (Built-in) Value Types
You can define your own value types using the struct keyword. StructuresIn C#, a struct is a simple user-defined type, a lightweight alternative that is quite different from a class. While structures do support properties, methods, fields, and operators, structures do not support inheritance or destructors. A struct is passed by value rather than by reference. A struct cannot derive from anything other than System.ValueType and it is instantiated without using a new operator. An example lightweight data type may be a set of coordinates. public struct Coords
{
public int X, Y;
public Coords(int p1, int p2)
{
X = p1;
Y = p2;
}
// Override the ToString method so the value appears in write
// statements.
public override string ToString()
{
return String.Format("({0},{1})", X, Y);
}
}
Reference TypesReference types sit on the stack, but they hold the address of an object on the heap, much like pointers in C++. Reference types include objects and strings. Passing Parameters by ValueWhen you pass a variable to a method, this means you are passing a copy of the variable to the method. Any change to the parameter that takes place inside the method has no effect on the original data stored in the variable. In the following example, the value of myInt will still be five after the method has been called. public void SquareIt(int x){ x *= x; }
public static void Main()
{
int myInt = 5;
SquareIt(myInt); // Passing myInt by value.
}
Passing Parameters by ReferenceIf you want the called method to change the value of the parameter, you have to pass it by reference, using the ref or out keywords. After call, myInt equals 25. public void SquareIt(ref int x){ x *= x; }
public static void Main()
{
int myInt = 5;
SquareIt(ref myInt); // Passing myInt by reference.
}
The ref keyword causes the argument to be passed by reference. Any changes made to the parameter in the method body will be reflected in that variable when control passes back to the calling method. An argument passed to a ref parameter must first be initialized. The out keyword works in the same manner as the ref keyword, but the argument passed to an out parameter does not have to be initialized first.
|