c# - Is it possible to make a list of generics that return a specific data type? -


class {  }  class b<t>:a {  public t foo; }   list<a> foo2 = new list<a>();  b<int> foo3 = new b<int>();  foo3.foo = 4;  foo2.add(foo3); 

now foo2[0].foo won't work since class not have property. want make list can have bunch of generic items.

currently converting type strings or byte array. there way create list of generic items return specific type back?

for solution without type casts, should take @ accepted answer question: discriminated union in c#

the union3 (or 4 or 5 or how many different types need) type proposed juliet allow have list accepts types want:

    var l = new list<union3<string, datetime, int>>  {             new union3<string, datetime, int>(datetime.now),             new union3<string, datetime, int>(42),             new union3<string, datetime, int>("test"),             new union3<string, datetime, int>("one more test")     };          foreach (union3<string, datetime, int> union in l)         {             string value = union.match(                 str => str,                 dt => dt.tostring("yyyy-mm-dd"),                 => i.tostring());              console.writeline("matched union value '{0}'", value);         } 

see here complete example: http://ideone.com/wzqhib


Comments

Popular posts from this blog

Detect support for Shoutcast ICY MP3 without navigator.userAgent in Firefox? -

web - SVG not rendering properly in Firefox -

java - JavaFX 2 slider labelFormatter not being used -