What is the meaning of Boxing in C # (unlike Java)?

I am learning C # from Java background and had some confusion regarding ValueType s. My understanding from reading MSDN's C # vs Java Overview was that primitives are objects instead of wrappers. If so, why should they be boxed to call methods? They seem to mean something other than Java autoboxing, but I'm not sure what. It is more like casting.

+6
source share
1 answer

Boxing is very similar to Java and C #. The difference is when this happens:

 Character ch = 'a'; 

this will lead to boxing in Java because "a" is primitive and Character is a class (wrapper). In C #, this is:

 Char ch = 'a'; 

will not lead to boxing, because Char not a primitive type, but a value type class. To call boxing in C #, you need to pass an object of type value to object .

 object o = 'a'; 

Change As mentioned in a HighCore comment, in C # there is an important boxing mechanism. Entering content in a List<int> does not cause a box and does not produce unnecessary messages, because List of int is a real list of unboxed ints.

+10
source

Source: https://habr.com/ru/post/978280/


All Articles