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
Explain the difference between .net and c#?
Is stringbuilder better than string?
What is difference between abstraction and encapsulation in c#?
What are the benefits of using the aggregate method in linq?
What is difference between variable and property in c#?
how to insert the data from the grid view to database table though button click.pls send the answer to mail id suri1319@gmail.com
What is namespace explain with example?
What are class fields?
Can c# inherit multiple classes?
what are windows services?
Can delegates be used as callbacks?
What are the different types of constructors?
Suppose you have already existing application with Visual Studio 6 (VB 6, InterDev 6) and this application utilizes Windows 2000 COM+ transaction services. With this example how can you approach migrating this application to .NET?
Where’s global assembly cache located on the system?
How do you declare an arraylist?