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
Define multicast delegate? How it be used?
Explain about Oops concept
What is the use of base keyword? Tell me a practical example for base keyword’s usage?
What is ienumerable <> in c#?
What is hashset c#?
Difference between directcast and ctype.
What is managed code?
What is the difference between returning iqueryable vs ienumerable?
What is a dynamic assembly?
What is delegate in c# interview questions?
Why are strings immutable c#?
How many types of variables are there in c#?
What is an interface class in c#?
If a class derives from another class, will the derived class automatically contain all the public, protected, and internal members of the base class?
What is an xsd file?