CSharp/C# Tutorial - C# Types






A type defines the blueprint for a value. There are different operations associated with different types.

In the following example, we used two literals of type int with values 2 and 3.


static void Main() {
   int x = 2 * 3;
   Console.WriteLine (x); 
} 

int is a buildin type to represent a integer value.

C# also defined types such as long, string, byte...

Variable

We declared a variable of type int whose name was x in the code above.

A variable represents a memory location that can contain different values. We can assign different value to a variable.

In contrast, a constant always represents the same value:


const int y = 6; 

The code above defined a constant value with int type.

All values in C# are instances of a type.





Predefined Type Examples

Predefined types are types that are supported by the compiler.

The int type in the example above is a predefined type for representing the set of integers, from -231 to 231-1.

We can perform functions such as arithmetic with instances of the int type as follows:


int x = 2 * 3; 

C# has other types. C# type string represents a sequence of characters, such as ".NET".

We can work with strings by calling functions on them as follows:


string message = "Hello world"; 
string upperMessage = message.ToUpper(); 
Console.WriteLine (upperMessage); 

The code above generates the following result.


int x = 2; 
message = message + x.ToString(); 
Console.WriteLine (message); 

The code above generates the following result.





Value Types Versus Reference Types

All C# types fall into the following categories:

  • Value types
  • Reference types
  • Generic type parameters
  • Pointer types

Value types include most built-in types for example, int, long, bool, as well as custom struct and enum types.

Reference types include all class, array, delegate, and interface types. It also includes the predefined string type.

Value types

The content of a value type variable or constant is a value.

We can define a custom value type with the struct keyword:


public struct Point { 
    public int X, Y; 
} 

The assignment of a value-type instance always copies the instance.

For example:


static void Main() { 
    Point p1 = new Point(); 
    p1.X = 1;//from w w  w .  j  a  va 2 s  .com
    Point p2 = p1; // Assignment causes copy

    Console.WriteLine (p1.X); // 1 
    Console.WriteLine (p2.X); // 1

    p1.X = 2; // Change p1.X

    Console.WriteLine (p1.X); // 2 
    Console.WriteLine (p2.X); // 1 
} 

Reference types

A reference type haves two parts: an object and the reference to that object.

The content of a reference-type variable is a reference to an object that contains the value.

Here is the Point type rewritten as a class:


public class Point { 
   public int X, Y; 
} 

Assigning a reference-type variable copies the reference, not the object instance.

This allows multiple variables to refer to the same object.


static void Main() { 
    Point p1 = new Point(); 
    p1.X = 1;//w ww  . ja va 2 s  .  com

    Point p2 = p1; // Copies p1 reference

    Console.WriteLine (p1.X); // 1 
    Console.WriteLine (p2.X); // 1


    p1.X = 2; // Change p1.X

    Console.WriteLine (p1.X); // 2 
    Console.WriteLine (p2.X); // 2 
} 

Null

A reference can be assigned the literal null, indicating that the reference points to no object:


class Point {...} 

Point p = null; 
Console.WriteLine (p == null); // True 

A value type cannot ordinarily have a null value:

Predefined Type Taxonomy

The predefined types in C# are:

  • Value types
    • Numeric
    • Signed integer (sbyte, short, int, long)
    • Unsigned integer (byte, ushort, uint, ulong)
    • Real number (float, double, decimal)
    • Logical (bool)
    • Character (char)
  • Reference types
    • String (string)
    • Object (object)

Predefined types in C# alias Framework types in the System namespace.

There is only a syntactic difference between these two statements:


int i = 5; 
System.Int32 i = 5;