c# - List of generic interfaces -


if have generic interface couple of implementing classes such as:

public interface idataelement<t> {     int dataelement { get; set; }     t value { get; set; } }  public class integerdataelement : idataelement<int> {     public int dataelement { get; set; }     public int value { get; set; } }  public class stringdataelement : idataelement<string> {     public int dataelement { get; set; }     public string value { get; set; } } 

is possible pass collection of implementing classes of differing types, without having resort passing object.

it not appear possible define return values as

public idataelement<t>[] getdata() 

or

public idataelement<object>[] getdata()  

any suggestions?

you can declare:

public idataelement<t>[] getdata<t>() 

and

public idataelement<object>[] getdata() 
  • although latter isn't you're after (your interface won't variant in c# 4 uses t in both input , output position; if variant, wouldn't able use variance value types). former require caller specify <t>, e.g.

    foo.getdata<string>();

is okay you?

there's no way of expressing "a collection of object, each of implements idataelement<t> different t" unless give nongeneric base class, @ use ilist<idataelement>. in case nongeneric idataelement have dataelement property, leaving value property in generic interface:

public interface idataelement {     int dataelement { get; set; } }  public interface idataelement<t> : idataelement {     t value { get; set; } } 

is useful in particular situation?

it's not clear how you'd want use collection of data elements without knowing types... if above doesn't you, maybe more expected collections.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -