Boxing and unboxing is also casting? - c#

When we convert the data types between primitive data types it is called as data type casting.
But when convert between ValueType and ReferenceType we call it as boxing and unboxing.
Can boxing and unboxing also be called casting?

Boxing is just wrapping a value type in an object hull, essentially. It doesn't actually involve a type conversion as, say, (int)3.14 does. Even though they both use the cast operator.

C# Type System contains three Types , they are Value Types , Reference Types and Pointer Types. C# allows us to convert a Value Type to a Reference Type, and back again to Value Types . The operation of Converting a Value Type to a Reference Type is called Boxing and the reverse operation is called Unboxing.
Boxing
int Val = 1;
Object Obj = Val; //Boxing
The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. From the above operation (Object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing.
UnBoxing
int Val = 1;
Object Obj = Val; //Boxing
int i = (int)Obj; //Unboxing
The first two line shows how to Box a Value Type . The next line (int i = (int) Obj) shows extracts the Value Type from the Object . That is converting a value of a Reference Type into a value of a Value Type. This operation is called UnBoxing.
Boxing and UnBoxing are computationally expensive processes. When a value type is boxed, an entirely new object must be allocated and constructed , also the cast required for UnBoxing is also expensive computationally

Boxing is the process to converting a value type to the type object and unboxing retriveing the value back http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
Well as casting is to convert one type to another compatible type

Casting: (it's basically about converting instance of one type into another)
int a = (int) 3.14 // from the example above does casting with loosing precision.
double b = a; // casting again (we may write (double) a - gives the same result).
Boxing: (process of copying value type to the heap)
object c = new MyStruct(); // MyStruct = struct.
object d = 1;
object e = (int) 3.14; // here we have actually casting and then boxing.
Unboxing (copying boxed value type back to the stack):
Button f = (MyStruct) c;
int g = (int) d; // it still unboxing however it looks exactly like the line #1 above.
Hope it helps.

Boxing is basically boxing a value type into a anonymous object which later can be unboxed.
int x = 567;
object y = x; //boxing
int z = (int) y; //z will be 123
int x = 567;
object y = (object)x; //explicit boxing (not necessary)
int z = (int) y; //z will be 123
Boxing/Unboxing should not be confused with type casting as while boxing we are just putting a wrapper around a variable. By doing the type casting you are actually changing the type of the variable or object.
Double x = 3.14444;
Integer y = (Integer)x; //(type cast or static cast in c++) changing the type and loosing the original value too in this case.
int x = 567;
object y = (object)x; //explicit boxing (not necessary)
float z = (float) y; //another example of type casting while unboxing

Every non-nullable value type has an associated heap-object type with identical members. Boxing a value type storage location creates a new heap-type instance and copies all the fields from the value-type storage location to the corresponding fields of the new instance. Unboxing a value type copies all the fields from an instance of its corresponding heap-object type to the corresponding fields of a value-type storage location.

Related

Boxing and UnBoxing from List to Object in C#

I wish to know my approach is right or wrong ?
Refer the two statements
Case: #1
List<string> person = new List<string>() {
"Harry"
"Emma"
"Watson"
}
Case: #2
Object person = new List<string>() {
"Harry"
"Emma"
"Watson"
}
Let me know
which statement is boxing and which statement is un-boxing ?
Both statements are equal and identical ???
There is no boxing because List is a reference type:
Boxing is the process of converting a value type to the type object or
to any interface type implemented by this value type
Unboxing extracts the value type from the object. Boxing is implicit;
unboxing is explicit. The concept of boxing and unboxing underlies the
C# unified view of the type system in which a value of any type can be
treated as an object.
Read More : MSDN
This is boxing:
int i = 123;
// The following line boxes i.
object o = i;
This is unboxing:
o = 123;
i = (int)o; // unboxing
None of them.
Boxing and unboxing is a mechanism provided to handle value types with unified type system in .NET.
for example:
int i = 4;
object o = i; //boxing, int is boxed in an object
int j = (int)o; //unboxing, an int is unboxed from an object
From MSDN:
Read more about why we need boxing and unboxing.
There is a special case in boxing of nullable types.
When a nullable type boxes, it boxes as its value or null. You can not have a nullable value boxed.
int? a = 4;
object o = a; //boxing, o is a boxed int now
Console.WriteLine(o.GetType()); //System.Int32
int? b = null;
o = b; //boxing, o is null
Console.WriteLine(o.GetType()); // NullReferenceException
It is not an boxing or unboxing.
Boxing means that you store the value type into the reference type, and the unboxing is the reverse of boxing.
In your examples both List and Object are reference type.You are only playing with references
int i = 123;
// The following line boxes i.
object o = i;
o = 123;
i = (int)o; // unboxing
int -> object boxing
object -> int unboxing
For more see here Boxing and Unboxing

Is it possible to unbox an object to more than one value type?

I have read in CLR via C#:
Unboxing is really just the operation of obtaining a pointer to the
raw value type (data fields) contained within an object.
which means that if more than one value type is contained within an object they can be unboxed using the below syntax:
int a= (int) o; //assigns a to 10
char b= (char) o; // assigns b to 'b'
How to implement such object which supports multiple unboxing?
A boxed value can only be the boxed form of a single type - if you call o.GetType() you'll find out what that is.
In general, you can only unbox to the exact same type, with a few wrinkles:
You can unbox an enum value to its underlying integral type, and vice versa
There's no such thing as a boxed nullable value type - boxing will result in either a boxed form of the non-nullable type, or a null reference. You can unbox to a nullable value type, and the result will either be a null value (if the original reference was a null reference) or a non-null value containing the unboxed non-nullable value, if you see what I mean.
So for example:
object o = 10;
FileMode mode = (FileMode) o; // Valid conversion to enum
int? n = (int?) o; // n has a value of 10
n = null;
o = n; // o's value is a null reference
n = (int?) o; // n's value is the null int? value
Are you talking about casting? In .NET if you have a boxed type it has a specific type and you can only cast it to the actual value type to unbox:
object o=10; // o is a boxed int with value 10
var a=(int)o; // works
//var b=(byte)o; // ERROR, this is not what's contained in it
var b=(byte)(int)o; // works, you get the int out then cast it to byte
var b=Convert.ToByte(o); // also works, using IConvertible
However, the main purpose of generics (initially at least) was to avoid boxing value types at all because of the performance costs involved. You can change most instances of object to generic values and preserve the types, and when you use them you'll have the proper value type right away.
If you have a custom object with for example both an int or a string, you could do this with explicit conversion operators.
Example;
public class CustomObject
{
int Number;
string Text;
public CustomObject(int number, string text) //constructor
{
Number = number;
Text = text;
}
public static explicit operator int(CustomObject o)
{
return o.Number;
}
public static explicit operator string(CustomObject o)
{
return o.Text;
}
}
Fiddle example

Does a ValueType get boxed when is declared as part of a class?

Considering this class:
public class Foo
{
public Int32 MyField;
}
I guess the "MyField" member is not on the thread stack because as it could be accessed by several threads, it has to be definitely in the managed heap, but does it means it is boxed and unboxed everytime it is used?
Thanks in advance
No, it is not boxed every time it is used. Boxing only occurs when you are coercing a value type into a reference type - it really has nothing to do with where the actual memory for the value was allocated (or even if any memory was allocated).
In your case, it's how you act on MyField that will determine if it's boxed, not how Foo is treated.
//No Boxing
var f = new Foo();
f.MyField = 5;
int val = f.MyField;
//Boxing
var f = new Foo();
f.MyFIeld = 5;
object val = f.MyField;
Note that in the second example val now contains a reference to a boxed int. MyField is still (and will always remain) an unboxed int and can be accessed without unboxing (thanks for pointing out the needed clarification, LukeH)
No, the value type is not boxed.
Boxing only occurs when you use a value type as though it is an object, for example when storing an int in an array of object. That is:
object[] a = new object[10];
int x = 1;
a[0] = x;
In that case, the value type is boxed.
But a value type stored as a field inside a class is not boxed.
No, boxing only occurs when a value type is treated as a System.Object (usually by implicit casting, i.e. passing it as a method parameter)
Value types only get boxed when they're assigned to a reference type variable (e.g. object). If you never assign MyField to anything other than an int or another struct to which it can be cast (e.g. double), it won't ever be boxed.

C# value type casting: how it works? [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Why does this conversion doesn't work?
Hi,
i discovered a strange behaviour of the framework.
This code throws an exception:
byte a = 1;
object b = a;
Console.WriteLine(b.GetType());
Console.WriteLine((byte)b);
Console.WriteLine((int)(byte)b);
Console.WriteLine(Convert.ToInt32(b));
Console.WriteLine((int)b);
The last line throws a System.InvalidCastException.
I'd like to know what are the mechanism in the framework that make this code illegal.
Is it a problem of boxing/unboxing?!
Yes, boxed value types can only be unboxed to the exact same type.
The variable b is a boxed byte.
When you do (int)(byte)b you're unboxing b back to a byte and then converting that unboxed byte to an int.
When you do (int)b you're attempting to unbox b directly to an int, which is illegal.
Edit...
As Jon mentions in his answer, there are cases where you don't have to unbox to the exact same type. Specifically:
A boxed T can be unboxed to Nullable<T>.
A boxed Nullable<T> can be unboxed to T, assuming that the nullable isn't actually null.
A boxed enum with an underlying type of T can be unboxed to T.
A boxed T can be unboxed to an enum with an underlying type of T.
Eric Lippert has a blog post on this.
Why? Because a boxed T can only be
unboxed to T.
Or Nullable< T >.
When you unbox, it has to be to one of the following:
The exact same type
The nullable form of the exact same type
If it's an enum type value, then you can unbox to the underlying type
If it's an integral type value, you can unbox to an enum which uses that underlying type
Examples:
using System;
class Test
{
enum Foo : short
{
Bar = 1
}
static void Main()
{
short x = 1;
object o = x;
short a = (short) o;
short? b = (short?) o;
Foo c = (Foo) o;
o = Foo.Bar;
short d = (short) o;
}
}
Anything else will give an exception. In particular, you can't unbox to a different type even if there's an implicit conversion from the actual type to your target type, which is what you're trying to do on the last line of your example.
You also can't unbox from an integral value to a nullable form of an enum with the same underlying type (or the reverse situation).
Note that if you box a nullable value type value, the result is either null (if the original value was the null value for the type) or the boxed non-nullable value... there's no such thing as a "boxed nullable value type" if you see what I mean.
Is it a problem of boxing/unboxing?
Yes. bis a boxed byte. So you need to unbox it to a byte first , like in (int)(byte)b

Different Cast Types in C# [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicates:
Casting: (NewType) vs. Object as NewType
Why is the C# “as” operator so popular?
Hey,
I know this may be a silly question but this doubt came to me today.
What is the difference between doing
String text = (String) variable;
and
String text = variable as String;
?
A cast can do three things:
Perform a user-defined conversion
Perform an unboxing conversion
Perform a reference conversion
An as operation is almost always a reference conversion, the only exception being unboxing to a nullable type:
object x = "hello";
int? y = x as int?; // y is null afterwards
Then there's the behaviour with conversions which fail at execution time. So the differences are:
Casts performing reference conversions or unboxing will throw InvalidCastException on failure; as will result in the null value of the target type instead
Casts can perform user-defined conversions; as can't
Casts can unbox to non-nullable value types; as can only be used for unboxing if the target type is a nullable value type
as will return null if variable isn't actually of that type (String in this case). The cast will throw an exception.
Here is the link to Eric Lippert's blog on casting in C#. I'd summarize it, but it's pretty short and he'll explain it much better than me.
http://blogs.msdn.com/ericlippert/archive/2009/10/08/what-s-the-difference-between-as-and-cast-operators.aspx
And here is his post on the cast operator:
http://blogs.msdn.com/ericlippert/archive/2009/03/19/representation-and-identity.aspx
There are a lot of different ways to cast in C#.
This will try to cast the reference to a String reference. If the cast fails, it throws an exception:
string text = (String) variable;
This will try to cast the reference to a String reference. If the cast fails, it will return a null reference to be assigned to the variable:
string text = varible as String;
This will cast a string reference to an object reference, which is a safe casting as String inherits from Object:
object text = (object)"1337";
Casting to a parent class can also be done implicitly:
object text = "1337";
This will box a value inside an object, then unbox it to a plain value again:
int value = 42;
object boxed = (object)value;
int valueAgain = (int)boxed;
The boxing can also be done implicitly:
int value = 42;
object boxed = value;
int valueAgain = (int)boxed;
This will make a widening conversion from byte to int:
byte a = 42;
int b = (int)a;
The same works as an implicit conversion:
byte a = 42;
int b = a;
This will make a narrowing conversion from int to byte, throwing away the overflow:
int a = 512;
byte b = (byte)a; // b now contains 0

Categories

Resources