As Keyword, conversion of derived class to base class type
-
Here is example code I made for this question:
using System;
namespace TestingAs
{
class baseClass
{
public string foo
{
get;
set;
}
}
class derivedClass : baseClass
{
public string bar
{
get;
set;
}
}class Program { static void Main(string\[\] args) { derivedClass derived = new derivedClass(); baseClass b = derived as baseClass; Console.WriteLine("type is {0}", b.GetType()); Console.ReadLine(); } }
}
I also tried just casting
baseclass b = (baseClass)derived;
which gives the same result: type is TestingAs.derivedClass Whyb
is not type ofbaseClass
? How would I "convert" my derived class into base class type? Thank you in advance! -
Here is example code I made for this question:
using System;
namespace TestingAs
{
class baseClass
{
public string foo
{
get;
set;
}
}
class derivedClass : baseClass
{
public string bar
{
get;
set;
}
}class Program { static void Main(string\[\] args) { derivedClass derived = new derivedClass(); baseClass b = derived as baseClass; Console.WriteLine("type is {0}", b.GetType()); Console.ReadLine(); } }
}
I also tried just casting
baseclass b = (baseClass)derived;
which gives the same result: type is TestingAs.derivedClass Whyb
is not type ofbaseClass
? How would I "convert" my derived class into base class type? Thank you in advance!This is the same concept of Reference Copying. In runtime the pointer of the object is set to derived class object. Thus it will always hold the derivedclassobject even if you cast it to base class object. :)
Abhishek Sur
My Latest Articles **Create CLR objects in SQL Server 2005 C# Uncommon Keywords Read/Write Excel using OleDB
**Don't forget to click "Good Answer" if you like to.
-
Here is example code I made for this question:
using System;
namespace TestingAs
{
class baseClass
{
public string foo
{
get;
set;
}
}
class derivedClass : baseClass
{
public string bar
{
get;
set;
}
}class Program { static void Main(string\[\] args) { derivedClass derived = new derivedClass(); baseClass b = derived as baseClass; Console.WriteLine("type is {0}", b.GetType()); Console.ReadLine(); } }
}
I also tried just casting
baseclass b = (baseClass)derived;
which gives the same result: type is TestingAs.derivedClass Whyb
is not type ofbaseClass
? How would I "convert" my derived class into base class type? Thank you in advance!Convert.ChangeType
? Why would you want to? -
Here is example code I made for this question:
using System;
namespace TestingAs
{
class baseClass
{
public string foo
{
get;
set;
}
}
class derivedClass : baseClass
{
public string bar
{
get;
set;
}
}class Program { static void Main(string\[\] args) { derivedClass derived = new derivedClass(); baseClass b = derived as baseClass; Console.WriteLine("type is {0}", b.GetType()); Console.ReadLine(); } }
}
I also tried just casting
baseclass b = (baseClass)derived;
which gives the same result: type is TestingAs.derivedClass Whyb
is not type ofbaseClass
? How would I "convert" my derived class into base class type? Thank you in advance!Think about it. When you derive a class from baseClass, you are adding information to baseClass. When you try to convert your derived class to a baseClass, you would have to throw information away. You could not recover the information by re-casting your item to a derivedClass, because otherwise your program would have to behave very differently with the following :
derivedClass d = new derivedClass();
baseClass b = d as baseClass;
derivedClass d2 = (derivedClass) b;and
baseClass b = new baseClass();
derivedClass d2 = (derivedClass) b;If the definitions were in (say) different methods to the casts, it could get very difficult to work out what is going on. Since wherever you can use a baseClass, you can use any derived class, C# is not keen on throwing away information, which could not be recovered by a re-cast back to the derived class.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones
-
Convert.ChangeType
? Why would you want to?Does it matter why I want to? My app is using .dll's which are returning class arrays. My previous problem was that the application was completed and working well when it was required to use custom data in a way I found "good" solution to change my application to use derived class from the class type returned from the .dll layer, it just worked as long as I did not need to use same class array objects as parameters in .dll layer method calls.
-
Does it matter why I want to? My app is using .dll's which are returning class arrays. My previous problem was that the application was completed and working well when it was required to use custom data in a way I found "good" solution to change my application to use derived class from the class type returned from the .dll layer, it just worked as long as I did not need to use same class array objects as parameters in .dll layer method calls.
I didn't quite follow all that, but I still see no reason to downgrade the instances.