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 difference between continue and break in c#?
What is the difference between an integer and int?
Can a string be null c#?
What is xml c#?
What is default value of bool in c#?
What is form feed in c#?
Define encapsulation?
What is the symbol used for in c#?
How can you sort strings in array that are passed to method as arguments?
What are the return types in c#?
What are boxing and unboxing?
How do you define a predicate?
Is .net and c# the same?
What is difference between encapsulation and abstraction in c#?
What is difference between sleep () and wait ()?