Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've seen multiple questions like this but is it possible to find an external override to this error? By the way the error is:

Cannot derive from 'T' because it is a type parameter

Here is a basic example of the requested functionality:
C#
public class Mimic<T> : T //Error
{

}


I was thinking I could add the generic parameter to the (Project).Properties and then retrieve it for the inheritance.
Posted
Updated 21-Dec-13 1:18am
v2

1 solution

No, you can't.

The whole idea of Generics is that they are type unspecific - they are general purpose rather than linked to a specific class: so you can declare a List class that work as a collection of a generic class, and it will work the same with an integer and an SqlCommand.

Declaring a class is different: the compiler needs to know exactly what the base class is so that it can tell what properties, fields, methods and events your new class inherits - and what it needs to implement in order to successfully inherit from the base class. A generic declaration can;t do that, because it doesn't know at compile time what the base class is going to be.

If you could do this, then you could do all sorts of very nasty things, like create a class that derives from an abstract class but which doesn't implement any of its abstract methods... X|
 
Share this answer
 
Comments
IAmABadCoder 21-Dec-13 7:34am    
Thankyou for the answer!
OriginalGriff 21-Dec-13 7:37am    
You're welcome!
Karthik_Mahalingam 21-Dec-13 8:08am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900