Reputation: 47
I would like to know what happens to the object when we do a cast. Does the layout of the object gets restructured or does the method table changes or is it only to ensure my assignments don't break during runtime ?
Upvotes: 1
Views: 198
Reputation: 152521
Usually nothing happens to the underlying data - it just determines how the code is interacting with the object.
However - some "casts" actually do a conversion, meaning they change the underlying data of an object.
For example:
(double)(int)1.1;
converts 1.1 to an integer (which truncates to 1), then converts back to a double
, which in this case would return 1.0.
You can also define explicit conversions for reference types:
public static explicit operator Celsius(Fahrenheit fahr)
{
return new Celsius((5.0f / 9.0f) * (fahr.degrees - 32));
}
Fahrenheit fahr = new Fahrenheit(100.0f);
Console.Write("{0} Fahrenheit", fahr.Degrees);
Celsius c = (Celsius)fahr;
Note that technically the original data didn't change in either case - the cast returns a new object with new data.
Upvotes: 1
Reputation: 55720
As all others have pointed out a cast by itself doesn't change the object being cast.
That said, there is a point worth making about the ability to overload the behavior of explicit conversions, which appear at first glance as casts. If a type overloads an explicit conversion operator then it could (although it really shouldn't) change the object being converted.
public class EvilClass
{
public int SomeProperty { get; set; }
public static explicit operator SomeOtherType(B bObj)
{
obj.SomeProperty = -1;
return SomeOtherType.CreateFromEvilClass(bObj);
}
}
Then, to the user, this looks like a cast:
EvilClass evilClass = new EvilClass();
evilClass.SomeProperty = 42;
SomeOtherType sot = (SomeOtherType)evilClass; // looks like cast..
Console.WriteLine(evilClass.SomeProperty); // Will print -1, not 42
Upvotes: 2
Reputation: 10863
The honest answer is it depends. Some conversions types just involve boxing and unboxing, which just changes the way the code is interpreted at runtime. The underlying object doesn't actually change. This type of casting is involved with casts between, for example, from string
to object
(as a string
is an object
, as is everything in C#).
http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx
Alternatively, if there's custom/user-defined cast operators involved, then anything could happen. The cast could create a completely new object that is a representation of the original that was cast. An example of this kind of cast would be an int
to a string
.
http://msdn.microsoft.com/en-us/library/09479473.aspx
Upvotes: 2
Reputation: 61349
None!
All you are doing is "pretending" that it is a different type. Of course, if it doesn't inherit from that type (or is actually an instance of the type when downcasting) the cast fails and you get nothing (null) when using the "as" operator to cast, and explicit (C-Style) cast will throw.
Unless, that is, you are dealing with value types (especially number types). If you do the following:
double myDouble = 1.5f;
int myInt = (int)myDouble;
double myOtherDouble = (double)myInt;
You will get 1 for myInt and myOtherDouble, because the number is truncated when performing the conversion. Most of the other value types don't even support this kind of casting, because the conversion is not defined.
Upvotes: 0
Reputation: 59645
If you cast a reference type, nothing happens. It is just a runtime check that the object is actually of the specified type. In some situations - like Byte
to Double
- the value will change to match the new data type. Actually it will not change but you will get a new value of the new data type that represents the same value. If it comes to user defined conversions it is up to the implementation what happens.
Upvotes: 1
Reputation: 62093
Nothing. A cast changes the logical assumption what a pointer points to.
Nothing in the object does change - all what happens is that a check is done whether the objet is of the assumed type.
If you have A and B : A, and a = new A()
Then basically
var B = (B) a
asks the compiler to check that A is actually a B.
Now, if a is ever created as instance of C (C:A) then - this can and will blow at this point.
Upvotes: 1