CSharp Articles

Type Comparisons, Coversion , Boxing and Unboxing

Type Comparisons

Once you have a variable, you inevitably want to compare it with another variable using a comparison operator (==, !=, and so forth). However, you need to be aware that type comparison behavior varies, depending on whether you’re comparing two value types or two reference types. Value type comparisons are as you would expect them: the comparison returns true if the values held are identical. Reference type comparisons generally return true if the variables point to the same object. Therefore, the comparison of two objects that contain the same data would not be considered equal.

Interestingly, although string is a reference type, as we saw previously with assignment, string behaves as a value type. The equality operator is overridden so a string comparison returns true if the values being referred to are identical. String also includes a static Compare method that can be used to perform case-insensitive comparisons.

Boxing and Unboxing

Boxing is the name of the technique C# uses to convert a value type to object (or System.Object) and unboxing is the name given to the conversion from an object back to a value type. Any type, value, or reference can be assigned to an object without an explicit conversion. If the source for the assignment is a value type, C# allocates heap for the value’s data, and then assigns the reference to that data to the object. Boxing is used whenever a conversion to object is required, either because of explicit assignment or because of parameters being passed to a method call. Unboxing moves a value type from an object reference to a value type. Note that conversion rules apply to this assignment. In other words, a value type cannot be unboxed to a different type without an explicit cast.

using System;
class BoxingSample
        public static void Main()
            // box
            object objNum;
            int intNumber= 300;
            objNum = intNumber;
           Console.WriteLine(“o is {0}, type is {1}”, o, o.GetType());
           // now we unbox.
           int intCopy;
           intCopy= (int)objNum;
           intCopy+= 5;
           Console.WriteLine(“Copy is {0}”, intCopy);

Boxing preserves type safety and performance with an object-based type system.

Type Conversions

C# supports both implicit and explicit type conversions. Implicit conversions occur without requiring a cast, while explicit conversions require a cast. The compiler generates an error if an attempt is made to perform a supported explicit conversion without a cast operator or if an illegal explicit cast is attempted. For a listing of the valid conversions, check the Framework SDK documentation.

The conversion operation can be overridden so your custom classes can provide either implicit or explicit conversions. It is recommended that conversions which can have unintended side effects be overridden explicitly. The explicit cast should be a signal to the user that the conversion may not be perfect. Casting a long to an integer type, for example.

Categories: CSharp Articles

Leave a Reply