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.
CSharp Online TrainingUse the AddRange() method to append a second list to an existing list. list1. AddRange(list2);
Generic types do not have a common type or interface amongst concrete definitions by default.
Have your Setting<T>
class implement an interface (or derive from a common class) and create a list of that interface (or class).
public interface ISetting { }
public class Setting<T> : ISetting
{
// ...
}
// example usage:
IList<ISetting> list = new List<ISetting>
{
new Setting<int> { name = "foo", value = 2 },
new Setting<string> { name = "bar", value "baz" },
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With