Adding different type of generic objects into generic list?

In general, you'd have to either use a List : ValuePair { public T Value { get; set; } public object RawValue { get { return Value; } } } Then you can have a List>(); IEnumerable memoryStreams = null; // For simplicity IEnumerable networkStreams = null; // For simplicity IEnumerable streams = null; // For simplicity streamSequenceList. Add(memoryStreams); streamSequenceList. Add(networkStreams); streamSequenceList.

Add(streams) This isn't applicable in your case because: You're using a generic class, not an interface You couldn't change it into a generic covariant interface because you've got T going "in and out" of the API You're using value types as type arguments, and those don't work with generic variable (so an IEnumerable.

In general, you'd have to either use a List or create a non-generic base class, e.g. Public abstract class ValuePair { public string Name { get; set;} public abstract object RawValue { get; } } public class ValuePair : ValuePair { public T Value { get; set; } public object RawValue { get { return Value; } } } Then you can have a List. Now, there is one exception to this: covariant/contravariant types in C# 4. For example, you can write: var streamSequenceList = new List>(); IEnumerable memoryStreams = null; // For simplicity IEnumerable networkStreams = null; // For simplicity IEnumerable streams = null; // For simplicity streamSequenceList.

Add(memoryStreams); streamSequenceList. Add(networkStreams); streamSequenceList. Add(streams); This isn't applicable in your case because: You're using a generic class, not an interface You couldn't change it into a generic covariant interface because you've got T going "in" and "out" of the API You're using value types as type arguments, and those don't work with generic variable (so an IEnumerable isn't an IEnumerable).

Not unless you have a non-generic base-type ValuePair with ValuePair : 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; } }.

No, it is not possible. You could create, in your case, a base class ValuePair from which ValuePair derives. Depends on your purposes.

It's not possible as far as I know. The line: List list = new List(); you wrote in your sample is not providing a concrete type for T and this is the issue, once you pass it, you can only add object of that specific type.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions