Unboxing explicitly convert an object to a value type. - CSharp Custom Type

CSharp examples for Custom Type:Box Unbox

Introduction

P:The following code shows how to do the simple boxing and unboxing of a value.

Demo Code

using System;/*from ww  w.  j ava 2s. co m*/
class myApp
{
   public static void Main()
   {
      float val = 3.14F;      // Assign a value type a value
      object boxed = val;     // boxing val into boxed
      float unboxed = (float) boxed;  // unboxing boxed into unboxed
      Console.WriteLine("val: {0}", val);
      Console.WriteLine("boxed: {0}", boxed);
      Console.WriteLine("unboxed: {0}", unboxed);
      Console.WriteLine("\nTypes...");
      Console.WriteLine("val: {0}", val.GetType());
      Console.WriteLine("boxed: {0}", boxed.GetType());
      Console.WriteLine("unboxed: {0}", unboxed.GetType());
   }
}

Result


Related Tutorials