Adding different type of generic objects into generic list

前端 未结 4 1884
星月不相逢
星月不相逢 2020-12-03 22:01

Is it possible to add different type of generic objects to a list?. As below.

public class ValuePair
{        
    public string Name { get; set;}
          


        
相关标签:
4条回答
  • 2020-12-03 22:36

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

    0 讨论(0)
  • 2020-12-03 22:42

    it's not possible as far as I know.

    the line:

    List<ValuePair> list = new List<ValuePair>();
    

    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.

    0 讨论(0)
  • 2020-12-03 22:46

    In general, you'd have to either use a List<object> 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<T> : ValuePair
    {
        public T Value { get; set; }              
        public object RawValue { get { return Value; } }
    }
    

    Then you can have a List<ValuePair>.

    Now, there is one exception to this: covariant/contravariant types in C# 4. For example, you can write:

    var streamSequenceList = new List<IEnumerable<Stream>>();
    
    IEnumerable<MemoryStream> memoryStreams = null; // For simplicity
    IEnumerable<NetworkStream> networkStreams = null; // For simplicity
    IEnumerable<Stream> 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<int> isn't an IEnumerable<object>)
    0 讨论(0)
  • 2020-12-03 22:53

    Not unless you have a non-generic base-type ValuePair with ValuePair<T> : ValuePair (it would work for an interface too), or use List<object>. 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<T> : ValuePair
    {
        protected override object GetValue() { return Value; }
        protected override void SetValue(object value) { Value = (T)value; }
        public new T Value { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题