C# Generic List of Generic List of Multiple Types -


here abstraction , simplification of issue:

i have set of toys , corresponding box these toys. want user able specify largest type of toy box can hold:

public class box<t> {} 

then within box class want have generic list of toys, each toy contained within box have generic type:

public class box<t> {     public list<toy> = new list<toy>();     public bool whatever;      [member functions, constructors...]     [the member functions depend on t] } 

the toys class this:

public class toy<t> t : struct //t type {     public list<t> = new list<t>();     public string name;     public string color;      [member functions, constructors...] } 

i want able create toys many different types , insert them box specified type. i'd able add boxes returning box largest type.

i don't know how begin. list of generic class multiple types throwing me loop. read various articles using abstract class or interface, haven't found example or accomplishes similar i'm trying do.

any assistance provide appreciated.

the solution can in c# 4.0.

possible future clarification:

i want toy generic , accept argument @ instantiation because toy must have list member.

the nested list within toy main problem. want list within box holds toys, each toy has different type constructor.

update:

i fixed box box typo.

update 2:

toy<plastic> typlastic = new toy<plastic>("name1", blue, new plastic[] {0xffee00, 0xf34684, 0xaa35b2}); toy<wood> tywood = new toy<wood>("name2", grain, new wood[] {a1, f4, h7});  box<plastic> bxbox = new box<plastic>();//the box has ability hold both plastic , wood toys.  plastic > wood > paper 

final: ended removing requirement box generic. used reflection create dynamically typed toy. everybody.

the code you're building best understood if models reality well.

the way model "an of b" use generics. set of kinds of box can hold 1 kind of thing modelled box<t>. box can hold toys box<toy>. set of kinds of box can hold 1 kind of thing, , thing has toy box<t> t : toy.

so far good. concept of toy<t> doesn't map in real life. might have box of biscuits or box of toys, don't have toy of biscuits, toy of dolls or toy of giraffes. concept "toy of" doesn't make sense, don't model it.

a more sensible thing model "there general class of things called toys. there no 1 thing toy; every toy more specific kind of toy. ball toy. doll toy." model that:

abstract class toy {} class doll : toy {} class ball : toy {} 

you said

i want toy generic , accept argument @ instantiation because toy must have list member.

why? a toy not have list of things. don't model that. rather, box logically modelled list of toys inside box. (or, since box not apply ordering, , box contains unique toys, perhaps set of toys better.)

i want able create toys many different types , insert them box specified type.

ok. operation on box<t> void insert(t item). can put toy box of toys, can put doll box of dolls, cannot put ball box of dolls.

then i'd able add boxes returning box largest type.

you need more define "the largest type". if add box of dolls box of balls, result neither box of balls nor box of dolls. result box of toys.

here's how model this. have toy hierarchy. continue saying box of t implemented set of contents, , provides sequence of contents.

// haven't compiled this. class box<t> : ienumerable<t> {     private hashset<t> set = new hashset<t>();     public insert(t item) { set.add(item); }     public ienumerator<t> getenumerator() { return set.getenumerator(); }     public ienumerator ienumerable.getenumerator() { return this.getenumerator(); } 

all boring far. come interesting bit. this works in c# 4.

    public static box<t> mergeboxes(ienumerable<t> box1, ienumerable<t> box2)     {         box<t> box = new box<t>();         foreach(t item in box1) box.insert(item);         foreach(t item in box2) box.insert(item);         return box;     } } 

now can say

box<doll> dollbox = new box<doll>() { new doll() }; box<ball> ballbox = new box<ball>() { new ball() }; box<toy> toybox2 = box<toy>.mergeboxes(ballbox, dollbox); 

the result of merging box of dolls box of balls box of toys.

this last bit works because ienumerable<t> covariant in c# 4. in c# 3, trickier right; you'd have like:

box<toy> toybox2 = box<toy>.mergeboxes(ballbox.cast<toy>(), dollbox.cast<toy>()); 

does make sense?


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? -