A program that illustrates boxing and unboxing. | Lecture 4 - slide 15 : 29 Program 1 |
using System; public class BoxingTest{ public static void Main(){ int i = 15, j, k; bool b = false, c, d; Object obj1 = i, // boxing of the value of i obj2 = b; // boxing of the value of b j = (int) obj1; // unboxing obj1 c = (bool) obj2; // unboxing obj2 // k = i + obj1; // Compilation error // d = b && obj2; // Compilation error k = i + (int)obj1; d = b && (bool)obj2; Console.WriteLine("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", i, obj1, b, obj2, j, c, k, d); } } |