public void BoxingUnboxing() { double dType1 = 2.3; //value type in stack double dType2 = new double(); // value type in stack again object oType = dType1; //boxing. dType1 value will be copyed to heap. oType as a reference will be available in stack double dType3 = (double)oType; //unboxing. Cast to double neccessary int iType = (int)oType; //Runtime exception. Invalid cast WorkWithTypes refType = new WorkWithTypes(); //reference type. Declared at heap }
public void ObjectBaseClass() { int iType = 2; object oType = iType; WorkWithTypes refType = new WorkWithTypes(); Console.WriteLine(iType); //Outputs the 2 Console.WriteLine(iType.ToString()); //outputs 2 but interpreted as string type Console.WriteLine(oType.ToString()); //outputs 2 again, because underlying type is Int Console.WriteLine(refType.ToString()); //outputs full class name by default }
public void DefaultWord() { int iType = default(int); //0 int iType1 = 0; string strType = default(string); //string null string strType1 = string.Empty; //string empty constant string strType2 = ""; //string empty string object oType = default(object); //null object oType1 = null; WorkWithTypes refType = default(WorkWithTypes); //null WorkWithTypes refType1 = null; }