Can I unbox a string? - c#

What I understand unboxing is when I take a object and unbox it to valuetype like the MSDN example:
int i = 123;
object o = i;
o = 123;
i = (int)o; // unboxing
So I just was thinking, can a string be unboxed? I think, No it can't because there is no valuetype that can represent a string. Am I right?

You're right. A string can't be unboxed because only value types are subject to boxing and unboxing; a string is a reference type.

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

Boxing and unboxing is also casting?

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.

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.

can we convert value type to referance type in c# or .net supported language

hi can we convert value type to referance type in c# or .net supported language
like int is value type ...so can we convert it referance type
For what purpose?
object intAsRef = 32;
is an int as a reference type. But this is called boxing (see here) and is generally considered to be something to avoid rather than craved.
On the other hand, if you want to pass a value object by reference so that you can alter its value inside a method then you need to change the signature of the receiving method and the call:
public void ChangeValueOfInt(ref int input)
{
input = 4;
}
int a = 2;
ChangeValueOfInt(ref a);
//a now equals 4
That is the subject called boxing and unboxing.
int i = 5;
object o = i;
int j = (int)o;
etc.
Any value type can be converted to reference type, for example:
int myInt = 5;
object obj = myInt;
The process is called "boxing" in .NET, it refers to the fact that even value types inherit the reference type Object. Any type in .NET can be cast to Object thusly:
int myint = 0;
object myIntAsRefType = myInt;
You can do this with other reference types, as well:
Random rnd = new Random();
object rndAsObject = rnd;
Before you can use any of their own methods or properties, you must un-box them:
if(myIntAsRefType is int)
int myNewInt = myIntAsRefType as int;
Alternatively:
if(myIntAsRefType is int)
int myNewInt = (int)myIntAsRefType;
With the help of Boxing and UnBoxing you can convert any value type to reference type. Boxing would allow you to wrap your value type around Object and UnBoxing does the opposite.
Boxing
int i = 10;
Object obj = (Object)i;
UnBoxing
int j=(int)obj;

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