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
Can an int be negative c#?
Is concurrent queue thread safe?
How do I register my code for use by classic com clients?
Why do we use parameters in c#?
What is callback in c#?
Explain the ways to deploy an assembly?
Explain About web methods and its various attributes
Difference between Value type & reference types ? and give the example in .Net?
What is master page in asp net c#?
What are some examples of desktop applications?
What is gac? What are the steps to create an assembly and add it to the gac?
Is c# difficult to learn?
What is writeline?
What is the difference between null and string empty in c#?
What is instantiating a class?