If I have a list that holds the Object in it like this:
List<object> t = new List<object>();
t.Add(10);
t.Add("xyx");
in this case if I fetch back the list items do I need to unbox them?
"Unboxing" only occurs if you have used an object to hold a value type.
In your example, the t.Add(10); is indeed boxing an int value type as an object, so it will have to be unboxed when you access it.
However, the t.Add("xyx"); is adding a string reference type, so it will NOT be boxed and it will NOT need to be unboxed when you access it.
In both cases, however, you must cast the value to the correct type in order to access it as that type.
if you need treat them like objects of specific type, yes.
var o = t[0]; //this is object
var i = (int)t[0]; //this is int
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With