Did it possible to cast a generic type of derived class to
generic type of base class?
Answer Posted / karthikeyant
Yes, it is Possible, if both are same type , But it is not
possible if both are diffrent type, below a gave sample
code describe things.
Example:
class BaseClassGenrics<T>
{
}
class DerivedClassGenrics<T> : BaseClassGenrics<T>
{
}
//-- correct one
DerivedClassGenrics<int> derivedClassGenrics = new
DerivedClassGenrics<int>();
BaseClassGenrics<int> gaseClassGenrics = new
BaseClassGenrics<int>();
gaseClassGenrics = derivedClassGenrics;
//-- Wrong one
DerivedClassGenrics<int> derivedClassGenrics = new
DerivedClassGenrics<int>();
BaseClassGenrics<string> gaseClassGenrics = new
BaseClassGenrics<string>();
gaseClassGenrics = derivedClassGenrics;
You get error like this : Cannot implicitly convert
type 'DerivedClassGenrics<int>'
to 'BaseClassGenrics<string>'
Is This Answer Correct ? | 1 Yes | 0 No |
Post New Answer View All Answers
How can i Spawn a Thread
What the different phase/steps of acquiring a proxy object in webservice?
What is the use of inheritance in c#?
can you allow a class to be inherited, but prevent the method from being over-ridden?
How many types of constructors are available in c#?
If a child class instance is created, which class constructor is called first - base class or child class?
Major difference between Basic httpbinding and WShttpbinding?
Why dictionary is faster than list?
What is desktop example?
Are attributes inherited c#?
Is cli same as the clr?
What are the different types of classes?
How do generics work in c#?
What is application c#?
What is Custom attribute? How to create? If I'm having custom attribute in an assembly, how to say that name in the code?