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
What is a generic in c#?
What is the use of tuple in c#?
What is an abstract class c#?
To whom a method is accesssed if it is marked as protected internal ?
What is returned if you pass the value 12.34 to the parsefloat () function?
What do constructors do in c#?
What is hashtable in c# net with example?
What is master page in asp net c#?
What is the difference between throw and throw ex?
Why we use dll in c#?
Which is better python or c#?
What are the types of delegates in c#?
hi all.... i need code snippets for store and retrive tiff fromat images in sqlserver....... kindy provide it.......
What is difference between code access and role based security?
Can the accessibility of a type member be greater than the accessibility of its containing type?