Jun 7, 2012

Variable Types in C#.NET

In this post, I will describe Variable Types in C#.NET. As we all know, A variable can be compared to a storage room, and is essential for the programmer. Every variable has a type that represents what values can be stored in the variable.  So is in C#.NET. And C# is a type-safe language, and the the compiler guarantees that values stored in variables are always of the appropriate type.
Basicly speaking, there are two types of variable in C#.NET- value type and reference type.

Value type.

Value types directly contain a values. Assigning one value type variable to another copies the contained value. This differs from the assignment of reference type variables, which copies a reference(or simple address) to the object but not the object itself.

All value types are derived implicitly from the System.ValueType.For example, int is an alias of System.Int32. For a complete list of aliases, see Built-In Types Table

Unlike reference types, it is not possible to derive a new type from a value type. However, like reference types, structs can implement interfaces.

Unlike reference types, it is not possible for a value type to contain the null value. However, the nullable types feature does allow values types to be assigned to null

Each value type has an implicit default constructor that initializes the default value of that type. For information on default values of value types.
A picture of value type from msdn.

Reference Type:
Variables of reference types, referred to as objects, store references to the actual data. This section introduces the following keywords used to declare reference types: class,interface,delegate;
two built-in reference types: object,string.

No comments:

Post a Comment