C# 获取列表的所有元素值<;x、 y>;
我是新来的,我想要一些关于列表的帮助 实际上,我想将我的C# 获取列表的所有元素值<;x、 y>;,c#,.net,list,loops,C#,.net,List,Loops,我是新来的,我想要一些关于列表的帮助 实际上,我想将我的列表中的每个y元素添加到一个变量中。我知道,这可能很容易,但我被困在那一部分了 /// <summary> /// Number of cards in the deck /// </summary> public byte NbTotalCards { get { byte nbCards = 0; for (byte i = 0; i <= this.Lst
列表中的每个y
元素添加到一个变量中。我知道,这可能很容易,但我被困在那一部分了
/// <summary>
/// Number of cards in the deck
/// </summary>
public byte NbTotalCards
{
get
{
byte nbCards = 0;
for (byte i = 0; i <= this.LstCardsWithQt.Count; i++)
{
if (this.LstCardsWithQt[i].Qt != 0)
{
if(this.LstCardsWithQt[i].Qt.Equals(2))
nbCards += 2;
else
{
nbCards += 1;
}
}
else
{
nbCards += 0;
}
}
return nbCardss;
}
}
顺便说一句,我在this.LstCardsWithQt[I].Qt!=0
ArgumentOutofRangeXeption(“索引超出范围。必须为
非负数且小于集合的大小)
你以错误的方式循环浏览你的收藏。而不是
for (byte i = 0; i <= this.LstCardsWithQt.Count; i++)
(证明card.Qt
只有0到2之间的值,并且它仍然是相同的逻辑-我允许自己将sum的类型更改为int
,而不是byte
。如果这样做,还需要在文件开头使用System.Linq
。)您以错误的方式循环浏览您的收藏。而不是
for (byte i = 0; i <= this.LstCardsWithQt.Count; i++)
(证明
card.Qt
只有0到2之间的值,并且它仍然是相同的逻辑-我允许自己将sum的类型更改为int
,而不是byte
。如果这样做,还需要在文件开头使用System.Linq。在for循环中,您应该更改如果项目的Qt
为3或更多,您确定只希望将nbCards
增加1吗?为什么nbCards+=0代码>行?它没有任何用处。为什么使用Equals
来比较字节而不是=
?不是列表。如果项目的Qt
为3或更多,您确定只想将nbCards
增加1吗?为什么nbCards+=0代码>行?它没有任何用处。为什么使用Equals
来比较字节而不是=
?不是列表。这是正确的答案。我正要写这篇文章,然后看到了这个答案。这是正确的答案。我正要写这封信,然后看到了这个答案。
for (byte i = 0; i < this.LstCardsWithQt.Count; i++)
public int NbTotalCards
{
get
{
return LstCardsWithQt.Sum( card => card.Qt);
}
}