Variables of type object can accept values of any data type : Object Cast « Class Interface « C# / C Sharp






Variables of type object can accept values of any data type

 
using System;
class ObjectTest
{
   public int i = 10;
}

class MainClass2
{
   static void Main()
   {
      object a;
      a = 1;   // an example of boxing
      Console.WriteLine(a);
      Console.WriteLine(a.GetType());
      Console.WriteLine(a.ToString());

      a = new ObjectTest();
      ObjectTest classRef;
      classRef = (ObjectTest)a;
      Console.WriteLine(classRef.i);
   }
}

   
  








Related examples in the same category

1.Casting Objects
2.Downcast will fail.
3.This code raises an exception at run time because of an invalid cast
4.Casting objects: upcastCasting objects: upcast
5.Casting objects: downcastCasting objects: downcast