如何在List 中获取特定的派生对象?

时间:2022-09-03 07:11:08

Let's say I have a generic list of Fruit (List<Fruit> fruits = new List<Fruit>()). I then add a couple of objects (all derived from Fruit) - Banana, Apple, Orange but with different properties on the derived objects (such as Banana.IsYellow).

假设我有一个Fruit的通用列表(List fruits = new List ())。然后我添加了几个对象(全部来自Fruit) - Banana,Apple,Orange但在派生对象上具有不同的属性(例如Banana.IsYellow)。

List<Fruit> fruits = new List<Fruit>();
Banana banana1 = new Banana();
Banana banana2 = new Banana();
Apple apple1 = new Apple();
Orange orange2 = new Orange();

fruits.Add(banana1);
fruits.Add(banana2);
fruits.Add(apple1);
fruits.Add(orange1);

Then I can do this:

然后我可以这样做:

foreach(Banana banana in fruits)
    Console.Write(banana.IsYellow);

But at execution time of course this is not valid because there is no IsYellow-property on the Apple and Orange objects.

但是在执行时当然这是无效的,因为Apple和Orange对象上没有IsYellow属性。

How do I get only the bananas, apples, oranges etc from the List<Fruit>?

如何从List 中仅获取香蕉,苹果,橙子等?

5 个解决方案

#1


foreach(Banana b in fruits.OfType<Banana>())

#2


You could just do

你可以这样做

foreach(Fruit fruit in fruits)
{
   Banana b = fruit as Banana;
   if(b != null)
   {
      Console.Write(b.IsYellow);
   }
}

#3


Step 1: First you should make sub-list from Fruit list. To make the sub-list use Generic's FindAll() and Predicate function.

第1步:首先,您应该从Fruit列表中创建子列表。要使子列表使用Generic的FindAll()和Predicate函数。

Step 2: Later, in the sub-set you can iterate, which contains only 'Banana'

第2步:稍后,在子集中,您可以迭代,其中仅包含'Banana'

Here is the code

这是代码

Step1:

List<Fruit> fruits = new List<Fruit>();
Banana banana1 = new Banana();
Banana banana2 = new Banana();
Apple apple1 = new Apple();
Orange orange1 = new Orange();

fruits.Add(banana1);
fruits.Add(banana2);
fruits.Add(apple1);
fruits.Add(orange1);

//Extract Banana from fruit list
List<Fruit> bananaComb = fruits.FindAll(IsBanana);

//Now iterate without worring about which fruit it is
foreach (Fruit fruit in bananaComb)
{
    Console.WriteLine(((Banana)fruit).IsYellow);
}

Step 2: Here comes predicate function

第2步:谓词功能

//A Predicate function to determine whether its a Banana
static protected bool IsBanana(Fruit aFruit)
{
    return aFruit.GetType().Name == "Banana" ? true : false;
}

#4


Personally, I find this method more readable:

就个人而言,我发现这种方法更具可读性:

foreach(Fruit fruit in fruits)
{
   if (fruit is Banana)
   {
      Banana b = fruit as Banana;
      Console.Write(b.IsYellow);
   }
   else if (fruit is Apple)
   {
      // ...
   }
}

#5


adding another syntax, though .OfType< Banana >() is probably the best.

添加另一种语法,但.OfType ()可能是最好的。

foreach (Banana b in fruits.Where(x => x is Banana))

#1


foreach(Banana b in fruits.OfType<Banana>())

#2


You could just do

你可以这样做

foreach(Fruit fruit in fruits)
{
   Banana b = fruit as Banana;
   if(b != null)
   {
      Console.Write(b.IsYellow);
   }
}

#3


Step 1: First you should make sub-list from Fruit list. To make the sub-list use Generic's FindAll() and Predicate function.

第1步:首先,您应该从Fruit列表中创建子列表。要使子列表使用Generic的FindAll()和Predicate函数。

Step 2: Later, in the sub-set you can iterate, which contains only 'Banana'

第2步:稍后,在子集中,您可以迭代,其中仅包含'Banana'

Here is the code

这是代码

Step1:

List<Fruit> fruits = new List<Fruit>();
Banana banana1 = new Banana();
Banana banana2 = new Banana();
Apple apple1 = new Apple();
Orange orange1 = new Orange();

fruits.Add(banana1);
fruits.Add(banana2);
fruits.Add(apple1);
fruits.Add(orange1);

//Extract Banana from fruit list
List<Fruit> bananaComb = fruits.FindAll(IsBanana);

//Now iterate without worring about which fruit it is
foreach (Fruit fruit in bananaComb)
{
    Console.WriteLine(((Banana)fruit).IsYellow);
}

Step 2: Here comes predicate function

第2步:谓词功能

//A Predicate function to determine whether its a Banana
static protected bool IsBanana(Fruit aFruit)
{
    return aFruit.GetType().Name == "Banana" ? true : false;
}

#4


Personally, I find this method more readable:

就个人而言,我发现这种方法更具可读性:

foreach(Fruit fruit in fruits)
{
   if (fruit is Banana)
   {
      Banana b = fruit as Banana;
      Console.Write(b.IsYellow);
   }
   else if (fruit is Apple)
   {
      // ...
   }
}

#5


adding another syntax, though .OfType< Banana >() is probably the best.

添加另一种语法,但.OfType ()可能是最好的。

foreach (Banana b in fruits.Where(x => x is Banana))