C# Tutorial - Dissecting Our Second Application – Types


In 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 Types

Value 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

Type Range Size
bool A true or false value Byte
byte 0 to 255 Unsigned 8-bit integer
sbyte -128 to 127 Signed 8-bit integer
char A single Unicode character Unicode 16-bit character
short -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65,535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer
float A non-integer number to approx 7 significant digits (±1.5 × 10-45 to ±3.4 × 1038) 32-bit floating point number
double A non-integer number to approx 14 significant digits (±5.0 × 10-324 to ±1.7 × 10308) 64-bit floating point number
decimal A non-integer number to approx 28 significant digits (±1.0 × 10e-28 to ±7.9 × 10e28) 128-bit floating point number that has a greater precision and a smaller range, suitable for financial and monetary calculations.

You can define your own value types using the struct keyword.

Structures

In 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 Types

Reference 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 Value

When 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 Reference

If 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.


<< Previous Contents Next >>

© Publicjoe, 2008