Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've always thought that the explicit cast is just a way to tell the compiler that I know what I'm doing when it doesn't seem so
For example:
C#
static void Main()
       {
           object blabla = new Derived();
           AcceptABase(blabla); /* This call to the method won't work because the compiler sees that I have an object variable that points to something that will be evaluated at runtime (in this case a reference to a derived object) so it can't guarantee that this code will execute (because System.Object is more general than Base) so I have to tell it that I know what I'm doing

       }

       static void AcceptABase(Base b)
       {
       }


but I've read in a book on C# and .NET that the explicit cast is performed at runtime. I'm really confused. What's actually getting casted in runtime? Is it changing the type (and which type)?

What I have tried:

Well my question is actually not a real programming problem but instead just a question about explicit casting.
Posted
Updated 3-Apr-18 8:28am

Any cast - implicit or explicit - changes the type of an object: but it doesn't actually change the object at all, it just checks that the object is of a type suitable to cast, and that such a cast operation exists.

For example, explicit cast operators exist for int to decimal, double to float, float to byte, byte to integer, and so on. It is also possible to cast up or down the inheritance tree:
C#
class A {}
class B : A {}
class C : B {}
class D : A {}

All these will work:
C#
A a = new C();
B b = (C) a;
C c = (C) a;
This will compile, but will fail at run time:
C#
D d = (D) a;
because although D is derived from A and so is C, you can't cast an instance of C to a class D because there is no direct derivation, and no explicit cast operator.
It compiles because D is derived from A, so an A variable might be holding an instance of D, but will fail at run time if it isn't.

So yes, the cast is performed at runtime, but nothing gets actually changed - the system just checks that it's a legal cast which it can;t do at compile time.
 
Share this answer
 
Comments
Maciej Los 3-Apr-18 15:39pm    
5ed!
It's not changing the type of the instance. Casting just means "I'm going to treat this instance as this type", but this only works if the type you're casting it to is somewhere in the inheritance tree of the instance. Well, somewhere above the instances' original type.

So, say you have the following inheritance tree:
System.Object
  MyNamespace.MyBaseClass
    MyNamespace.MyFirstInheritedClass
      MyNamespace.MySecondInheritedClass
        MyNamespace.MyThirdInheritedClass

and you create an instance of MySecondInheritedClass, you can cast it to any type above the type you created, MyFirstInheritedClass, MyBaseClass, or Object. This is because MySecondInheritedClass implements everything in every type at and above it in the inheritance tree.

You can NOT cast it down the tree, meaning you cannot cast it to MyThirdInheritedClass. This is because the MySecondInheritedClass type doesn't implement anything the MyThirdInheritedClass added to the MySecondInheritedClass type.

Again, when you say "cast", either explicit or implicit, you're just saying "I'm going to treat this instance as if it were this type". When that happens, you only get access to the methods and properties of that new type, even when you pass that casted instance to another method.
 
Share this answer
 
Comments
Maciej Los 3-Apr-18 15:39pm    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900