Just another tech site

C# Type

Variable and Types


Value Type

Primitive types

see complete list in MSDN [3]

Reference Type

Variables of reference types, referred to as objects, store references to the actual data.


Stack and Heap

The distinction between stack and heap relates to programming. When you look at your computer memory,

[ref 1]

The stack is a place in the computer memory where all the variables that are declared and initialized before runtime are stored.
The heap is the section of computer memory where all the variables created or initialized at runtime are stored.

[ref 2]

Type Convertion

Implicit conversions

Implicit conversions: No special syntax is required because the conversion is type safe and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.[6]

// Implicit conversion. num long can
// hold any value an int can hold, and more!
int num = 2147483647;
long bigNum = num;

Explicit conversions (casts)

Explicit conversions (casts): Explicit conversions require a cast operator. Casting is required when information might be lost in the conversion, or when the conversion might not succeed for other reasons. Typical examples include numeric conversion to a type that has less precision or a smaller range, and conversion of a base-class instance to a derived class.[6]

double x = 1234.7;
int a;
// Cast double to int.
a = (int)x;

User-defined conversions

User-defined conversions: User-defined conversions are performed by special methods that you can define to enable explicit and implicit conversions between custom types that do not have a base class–derived class relationship.[6]

class SampleClass
    public static explicit operator SampleClass(int i)
        SampleClass temp = new SampleClass();
        // code to convert from int to SampleClass...

        return temp;

Conversions with helper classes

Conversions with helper classes: To convert between non-compatible types, such as integers and System.DateTime objects,[6]


string dateString = "5/1/2008 8:30:52 AM";
DateTime date1 = DateTime.Parse(dateString, 

Boxing and Unboxing

Boxing is the process of converting a value type to the type object or to any interface type implemented by this value type.
When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap. Unboxing extracts the value type from the object. Boxing is implicit; unboxing is explicit.[5]

int i = 123;
// The following line boxes i.
object o = i;
o = 123;
i = (int)o;  // unboxing


[1] http://www.maxi-pedia.com/what+is+heap+and+stack
[2] http://www.kdgregory.com/index.php?page=java.refobj
[3] http://msdn.microsoft.com/en-us/library/83fhsxwc(v=VS.100).aspx
[4] http://msdn.microsoft.com/en-us/library/490f96s2.aspx
[5] http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
[6] http://msdn.microsoft.com/en-us/library/ms173105.aspx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: