Saturday, June 15, 2013

C# Data Types

C# is a type-safe language Variables are declared as being of a particular type and each variable is constrained to hold only values of its declared type.
Variables can hold either value types or reference types, or they can be pointers. Here's a quick recap of the difference between value types and reference types.

• Where a variable v contains a value type, it directly contains an object with some value. No other variable v' can directly contain the object contained by v (although v' might contain an object with the same value).

• Where a variable v contains a reference type, what it directly contains is something which refers to an object. Another variable v' can contain a reference to the same object referred to by v.

Value Types

C# defines the following value types:

    Primitives int i;
    Enum enum state {off, on }
    Struct struct Point{int x, y; }

It is possible in C# to define your own value types by declaring enumerations or structs. These user-defined types are mostly treated in exactly the same way as C#'s predefined value types, although compilers are optimized for the latter. In C# all of the apparently fundamental value types are in fact built up from the (actually fundamental) object type.

Reference Types

The pre-defined reference types are object and string, where object - is the ultimate base class of all other types. New reference types can be defined using 'class', 'interface', and 'delegate' declarations. Therefore the reference types are:

  1. Predefined Reference Types
• Object                          • String

  1. User Defined Reference Types
• Classes                      • Interfaces
• Delegates                  • Arrays

Reference types actually hold the value of a memory address occupied by the object they reference.

No comments:

Post a Comment