Wednesday, May 12, 2010

C# Types, Value Types

Types.

-Value Type
-Reference Type

Value Type
-All value types are derived implicitly from the struct "System.ValueType".
-Variable that are of value types directly contain a values.
-Assigning one value type variable to another copies the contained value.
-It is not possible to derive a new type from a value type
-By default value types doesn't contain a null value. However, the nullable types feature does allow value types to be assigned to null.

Reference Type
-Reference type variables carries a reference to the object but not the object itself.
-Assigning one Reference type variable to another copies the reference only.
-Reference type can contain null by default.

---------------------------------------------------------------------------------------------------------------------------------------------
Categories of Value Type

Struct
Ref: http://msdn.microsoft.com/en-us/library/saxz13w4(VS.80).aspx

-A struct type is a value type that is typically used to encapsulate small groups of related variables.

eg:

public struct Book
{
public decimal price;
public string title;
public string author;
}

public struct PostalAddress
{
// Fields, properties, methods and events go here...
}



Differences between Struct & Class

-Structs share almost all the same syntax as classes, although structs are more limited than classes:

  • Within a struct declaration, fields cannot be initialized unless they are declared as const or static.

  • A struct may not declare a default constructor

  • Cannot declare a constructor with no parameters

  • Cannot declare a destructor.

  • Structs are value types while classes are reference types.

  • Unlike classes, structs can be instantiated without using a new operator.

  • Structs can declare constructors, but they must take parameters.

  • A struct cannot inherit from another struct or class, and it cannot be the base of a class. All structs inherit directly from System.ValueType, which inherits from System.Object.

  • A struct can implement interfaces.

-When a struct is initialized all the declared fields are initialized with default values.

Enum

--------------------------------------------------------------------------------------------------------------------------------------------
-Enums are typed constants.
-The default underlying type of the enumeration elements is int
-By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1.

Example

using System;
public class EnumTest
{
enum Days {Sat=1, Sun, Mon, Tue, Wed, Thu, Fri};

static void Main()
{
int x = (int)Days.Sun;
int y = (int)Days.Fri;
Console.WriteLine("Sun = {0}", x);
Console.WriteLine("Fri = {0}", y);
}
}

Output

Sun = 2
Fri = 7

---------------------------------------------------------------------------------------------------------------------------------------------
Boxing and Unboxing

Boxing
Ref:
Boxing http://msdn.microsoft.com/en-us/library/25z57t8s(VS.80).aspx

-Converting a value type to a reference type is called Boxing.
-It is of two types Implicit & Explicit.



eg :
int
i = 123;
object o = (object)i; //Explicit boxing


int i = 123;
object o = i; //Implicit boxing


Unboxing
Ref:
http://msdn.microsoft.com/en-us/library/b95fkada(VS.80).aspx

-Converting reference type to Value type is called Unboxing.
-Unboxing is always explicit only.
-Only a boxed value can be unboxed.
-Attempting to unbox null or a reference to an incompatible value type will result in an InvalidCastException.
-Checking the object instance to make sure it is a boxed value of the given value type.
-Copying the value from the instance into the value-type variable.


eg :
o = 123;
i = (int)o; // unboxing


---------------------------------------------------------------------------------------------------------------------------------------------

Nullable Types


  • Nullable types represent value-type variables that can be assigned the value of null. You cannot create a nullable type based on a reference type. (Reference types already support the null value.)

  • The syntax T? is shorthand for System.Nullable<T>, where T is a value type. The two forms are interchangeable.

  • Assign a value to a nullable type in the same way as for an ordinary value type, for example int? x = 10; or double? d = 4.108;



---------------------------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment