RSS

Tag Archives: casting

Boxing x Unboxing

Boxing and unboxing is not to be confused with downcast / upcast.

  • 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.
  • The concept of boxing and unboxing underlies the C# view that a value of any type can be treated as an object.

Example:

Boxing

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

Unboxing

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

Reference: MSDN

 
Leave a comment

Posted by on June 5, 2015 in General

 

Tags: , , , ,

Upcasting x Downcasting

Upcasting

Is when you cast an object to its base class type.

public class Person
{ }

public class President : Person
{ }

void Main()
{
    var president = new President();

    // Upcasting
    var person = (Person)president;
}

Downcasting

Well, it’s the opposite of upcasting, when you cast a reference of a base class to one of its derived classes.

You can only downcast when the object’s type is original a derived class, so it’s important to perform some checking prior, such as using as or is operators.

public class Person
{ }

public class President : Person
{ }

void Main()
{
    var president = new President();
    var person = (Person)president;

    // Downcasting
    var president = (President)person;
}
 
1 Comment

Posted by on April 27, 2015 in General

 

Tags: , , ,

Explicit Conversion X as Operator

What’s the difference between explicit conversion, as operator and calling a method to a conversion?

public void Test2()
{
    var obj = "stack247";

    var str1 = (string)obj;
    var str2 = obj as string;
    var str3 = obj.ToString();
}

Conclusion (as seen from the result below):

  • Explicit conversion throws InvalidCastException if obj is not a string type.
  • Explicit conversion assigns anything to obj, even if it’s null.
  • as operator returns null if obj is not a string type.
  • as operator can’t be use in value type because it’s not null-able, unless the value type is null-able (for example, int? will work)
  • ToString(), or whatever the method that return certain type, will throw NullReferenceException if obj is null.
  • ToString(), or whatever the method that return certain type, will return string representation of an object if the object can’t be converted.
public void Test1()
{
    var obj = "stack247";

    var str1 = (string)obj;
    // Return stack247

    var str2 = obj as string;
    // Return stack247

    var str3 = obj.ToString();
    // Return stack247
}
public void Test2()
{
    var obj = new object();

    var str1 = (string)obj;
    // Return InvalidCastException (Unable to cast object of type 'System.Object' to type 'System.String'

    var str2 = obj as string;
    // Return null

    var str3 = obj.ToString();
    // Return System.Object
}
public void Test3()
{
    var obj = 247;

    var str1 = (int)obj;
    // Return 247

    var str2 = obj as int;
    // Return The 'as' operator must be used with a reference type or nullable type ('int' is non-nullable value type).

    var str3 = obj as int?;
    // Return 247

    var str4 = obj.ToString();
    // Return 247
}
public void Test4()
{
    object obj = null;

    var str1 = (string)obj;
    // Return null

    var str2 = obj as string;
    // Return null

    var str3 = obj.ToString();
    // Return NullReferenceException (Object reference not set to an instance of an object).
}
 
1 Comment

Posted by on January 13, 2015 in General

 

Tags: , , ,

 
%d bloggers like this: