elena1234

Boxing and Unboxing in C#

Dec 10th, 2021 (edited)
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.54 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3.  
  4. namespace Boxing
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             // Pretend this is .Net 1.0
  11.             string chiken = "chicken";
  12.             int fish = 1;
  13.             ArrayList Truck = new ArrayList();
  14.             Truck.Add(chiken);
  15.             Truck.Add(fish); // boxing: value type into object type
  16.             int secondFish = 2;
  17.  
  18.             Truck[1] = (int) Truck[1] + secondFish; // unboxing: object type into value type
  19.         }
  20.     }
  21. }
  22.  
Add Comment
Please, Sign In to add comment