You can, of course, declare:
public IDataElement<T>[] GetData<T>()
and
public IDataElement<object>[] GetData()
although the latter is probably not what you need (your interface will not be an option even in C # 4, as it uses T both as input and output position, even if it is an option, you cannot use this variance for types of values). The first requires the caller to indicate <T> , for example.
foo.GetData<string>();
Is that good for you?
It is impossible to express a "collection of an object, each of which implements an IDataElement<T> for another T", unless you give it an unrelated base class in which you could simply use IList<IDataElement> . In this case, the non-generic IDataElement may have the DataElement property, leaving the Value property in the general interface:
public interface IDataElement { int DataElement { get; set; } } public interface IDataElement<T> : IDataElement { T Value { get; set; } }
Is this useful in your particular situation?
It is unclear how you want to use a collection of data elements without knowing their types ... if the above does not help you, perhaps you could say more about what you expect from collections.
Jon skeet
source share