c# - Inherited classes as function parameter -


i've got 2 classes (myclass1 , myclass2) both inherit mybaseclass.

i want write function takes list of either of these parameter, this

private void dostuff(list<mybaseclass> input)  {  ...  } 

how pass list function?

you can't quite that, list<myclass1> isn't list<mybaseclass>. search on stack overflow generic variance explanations of why that's case.

one thing can make generic:

private void dostuff<t>(list<t> input) t : mybaseclass 

alternatively, in c# 4 , .net 4, if need iterate on it, use ienumerable<t> covariant in t:

private void dostuff(ienumerable<mybaseclass> input) 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -