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 you use abstract class and interface?
How do I do implement a trace?
What do you mean by thread safe in c#?
In a memory when you Box and Unbox a value-type what happens?
Where is c# compiler located?
Why do we use threads in c#?
Difference between value and reference type. What are value types and reference types?
What is difference between property and variable in c#?
What are the Types of compatabilities and explain them
What is the purpose of namespace in c#?
Explain the difference between // comments, /* */ comments and /// comments?
What is anonymous method in c#?
What do you know about Translate Accelerator?
Why var is used in c#?
In which way you can convert a value-type to a reference-type?