Is it possible to add different type of generic objects to a list?. As below.
public class ValuePair
{
public string Name { get; set;}
Not unless you have a non-generic base-type ValuePair with ValuePair (it would work for an interface too), or use List. Actually, though, this works reasonably:
public abstract class ValuePair
{
public string Name { get; set; }
public object Value
{
get { return GetValue(); }
set { SetValue(value); }
}
protected abstract object GetValue();
protected abstract void SetValue(object value);
}
public class ValuePair : ValuePair
{
protected override object GetValue() { return Value; }
protected override void SetValue(object value) { Value = (T)value; }
public new T Value { get; set; }
}