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 Dependency of Injection?
Is equal in c#?
What is the difference between list and arraylist in c#?
How many bytes is a char c#?
Can hashtable have duplicate keys in c#?
How to reduce image resolution in C#?
What is expression tree with example?
what are nullable types in c#
Can constructor have return type c#?
Can a class be protected in c#?
Is typeof c#?
What is data type c#?
Explain manifest in c#.
You are designing a user control. You created new property called backgroundimage which is of type image. You wanted to disable storing this property in the user’s form. How to achieve this?
Enlist some of the properties of a thread class?