有谁知道,为什么在“股票适合”的部分强调(错误)套装(儿童类股票)?
//Picking Suit out of the Stock
public System.Collections.ArrayList Suit()
{
System.Collections.ArrayList array = new System.Collections.ArrayList(); //looping through Persons array
foreach (Stock stock in allStock)//using code snippets
{
if (stock is Suit) //if it is a customer, display value, if not, return to the array list
{
array.Add(stock);
}
}
return array;
}
答案 0 :(得分:1)
您的方法与您的子类名称相同:Suit
。这是错误。
这个方法应该重命名(并且可以使用LINQ重构),如下所示:
public List<Suit> GetSuits()
{
return
allStock
.Select(stock => stock as Suit)
.Where(suit => suit != null)
.ToList();
}
或没有LINQ:
public List<Suit> GetSuits()
{
var result = new List<Suit>();
foreach (Stock stock in allStock)
{
var suit = stock as Suit;
if (suit != null)
{
result.Add(suit);
}
}
return result;
}