Use cases for boxing a value type in C#?

后端 未结 9 1125
既然无缘
既然无缘 2021-02-05 23:36

There are cases when an instance of a value type needs to be treated as an instance of a reference type. For situations like this, a value

9条回答
  •  攒了一身酷
    2021-02-05 23:47

    Below is some examples of boxing/unboxing

    ArrayList ints = new ArrayList();
    myInts.Add(1); // boxing
    myInts.Add(2); // boxing
    
    int myInt = (int)ints [0]; // unboxing
    
    Console.Write("Value is {0}", myInt); // boxing
    

提交回复
热议问题