私は2つのリストを持っています。両方のリストの型は、同じ基本型から継承します。私はそれらを繰り返し、2つの基本的に同じforeachループを順番に持たずに基本クラスの機能を使う操作をしたい。(同じ基本クラスの)2つの継承クラスの2つのリストを反復する
リストが別のリストまたは何らかの並べ替えのものにコピーできません。操作が完了した後に元の形式でリストを別々に使用する必要があるためです。
機能を記述することなくこれを行う方法はありますか?
class Program
{
static void Main(string[] args)
{
// I have two lists of inherited classes
List<Babby1> list1 = returnBabby1();
List<Babby2> list2 = returnBabby2();
// I want to iterate through both, and do the same thing, which is a part
// of the base class functionality.
// Basically I want this to be a single foreach loop.
foreach (Babby1 item in list1)
item.var = 50;
foreach (Babby2 item in list2)
item.var = 50;
// I have to send them as separate lists, the type being the original (inherited) class
sendBabby1(list1);
sendBabby2(list2);
}
static void sendBabby1(List<Babby1> list)
{
}
static void sendBabby2(List<Babby2> list)
{
}
static List<Babby1> returnBabby1()
{
return new List<Babby1>();
}
static List<Babby2> returnBabby2()
{
return new List<Babby2>();
}
}
class Base
{
public int var;
}
class Babby1 : Base
{
public int var1;
}
class Babby2 : Base
{
public int var2;
}
次の2つの別々のリストを持っているので、あなたは、新しいリストを作成せずに1として、それらを列挙することはできません。 – slugster