Showing posts with label Types Of Boxing. Show all posts
Showing posts with label Types Of Boxing. Show all posts

Thursday, May 13, 2010

BOXING AND UNBOXING TYPES

BOXING AND UNBOXING TYPES

Boxing refers to converting a value type to an object type, and unboxing refers to the opposite.
Boxing is carried out implicitly in C#, use type casting to unbox to an appropriate data type.

For example
int i; Console.WriteLine("i={0}",i);

The WriteLine() method requires an object, so in the above statement integer i is implicitly boxed to an object and passed to the WriteLine method.

An example for unboxing is
    int i;
    object obj = i;  // boxing is implicit
    int j;
    j = (int) obj; // to unbox we use type cast

Typically unboxing is done in a try block. If the object being unboxed is null or if the unboxing cannot succeed because the object is of a different type, an InvalidCastException is thrown.

DELEGATES
A delegate essentially creates a name for a the specific type/signature of a method. Delegates are type safe function pointers.
One must first declare a delegate.

// delegates.cs
    using System;
// declare delegate with the signature of the  encapsulated method
    delegate void MyDelegate(string m,int a,int b);
    class Application
    {
        public static void Main()
        {
             MyDelegate md = new MyDelegate(FirstMethod);
             md += new MyDelegate(SecondMethod);
            md("message A",4,5);
            md("message B",7,11);
         } // end Main
}

    static void FirstMethod(string s1,int x1,int y1)
    {
         Console.WriteLine("1st method: " + s1);
         int sum1 = x1 + y1;
         Console.WriteLine("sum1 = " + sum1);
    }
   static void SecondMethod(string s2,int x2,int y2)
   {
         Console.WriteLine("2st method: " + s2);
          int sum2 = x2 + y2;
          Console.WriteLine("sum2 = " + sum2);
    }

OUTPUT :

1st method message A
sum1 = 9
2st method message A
sum2 = 9
1st method message B
sum1 = 18
2st method message B
sum2 = 18


TYPES :

The typeof command is an operator. It resolves at compile time and operates over a type. To check whether an object is compatible to a specific type is to apply the is keyword.

// myTypeof.cs
using System;
using System.Text;
class myTypeof
{
    public static void Main()
     {
             double b = 3.14;
             string s = "xxx";
             StringBuilder sb = new StringBuilder("123456789");
    }
        Type at = typeof(double);
        Console.WriteLine("at = {0}",at);
        Type st = typeof(string);
        Console.WriteLine("st = {0}",st);
        Type sbt = typeof(StringBuilder);
        Console.WriteLine("sbt = {0}",sbt);
        if(s is string)    
         {
            
Console.WriteLine(at);
          }
          if(s is StringBuilder)    
          {
                
Console.Write("s is of the StringBuilder");
           }
            else
            {
                 Console.Write("s is not of StringBuilder");
            }
        if(b is int)
        {
        Console.WriteLine("b is int");
         }
}