Alex Rivera | Logout

Can I create a List<Class<T>>?

Asked 2011-06-05T10:03:52.370
11

I have a class

public class Setting<T>
{
    public string name { get; set; }

    public T value { get; set; }
}

now I want to create an IList<Setting<T>> but with different types of Setting<T>'s T in it, I want e.G.

List<Setting<T>> settingsList;
settingsList.Add(new Setting<int>());
settingsList.Add(new Setting<string>()); 

I've tried IList<Setting<T>> but this seems not possible since the compiler doesn't find Type T.

I know that I could use object but I want it to be strongly typed. So my question is if there is a possibility of getting this working.

Edit
Report

1 Answer

0

You can use T only inside your class:

class Setting<T>
{
    // T is defined here
}

not outside. Outside you need to specify the concrete type:

Settings<string> stringSettings = new Settings<string>();

or

List<Settings<string>> list = new List<Settings<string>>();
list.Add(stringSettings);
answered 2011-06-05T10:13:13.633

Your Answer