UNeverNo
UNeverNo

Reputation: 583

Custom generic list of generic class

I got a class

public class ID_Name<T>
{
    public ID_Name(T id)
    {
        this.ID = id;
    }

    public T ID { get; set; }

    public string Name
    {
        get 
        {
            return Helper.SomeReturnValue;            
        }
    }
}

All I want to do is generate a custom List of ID_Name where I can pass an ID_Name.ID as parameter in Add. I tried the following:

public class ID_Name_List<T> : IList<T> where T : ID_Name<T>

but then I get the following error:

The type "EProtokollStatus" cannot be used as type parameter "T" in the generic type or method "ID_Name_List<\T>". There is no boxing conversion or type parameter conversion from "EProtokollStatus" in ID_Name<\EProtokollStatus>.

I read something about this issue here: No boxing or type parameter conversion for generic Type parameter but I can't see a restriction except ID_Name here. This may be wrong somehow, because everything I want to express is "use the same type T as in ID_Name for ID_Name_List", but how do I achieve that?

I found something here: C#: Declaring and using a list of generic classes with different types, how? but I don't want to create many different classes for all possible types.

All I want to achieve is something like

ID_Name_List<EProtokollStatus> myList = new ID_Name_List<EProtokollStatus>();
myList.Add(EProtokollStatus.ValueX);

Upvotes: 1

Views: 1855

Answers (1)

Dark Falcon
Dark Falcon

Reputation: 44181

Your current constraint makes no sense. Did you mean:

public class ID_Name_List<T> : IList<ID_Name<T>>

Upvotes: 4

Related Questions