c#将整数添加到列表中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13229265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c# add integer to list
提问by Optical
When I check if a textbox equals 1, I want to check if the list allready contains a specific value. When I run this, it allways goes to the else code. What am I doing wrong?
当我检查文本框是否等于 1 时,我想检查列表是否已经包含特定值。当我运行它时,它总是转到 else 代码。我究竟做错了什么?
List<int> list = new List<int>();
if (Convert.ToInt32(DobbelWaarde.Text) == 1)
{
if (list.Contains(1))
{
Console.WriteLine("1 is allready been chosen");
}
else
{
list.Add(1);
Console.WriteLine();
foreach (int li in list)
{
Console.WriteLine(li);
Console.WriteLine("We add 1");
}
}
}
回答by Habib
When I run this, it allways goes to the else code.
当我运行它时,它总是转到 else 代码。
You are not adding integer in your list anywhere. You start with an empty Listand then check if it contains 1that is why it goes to the else part.
您没有在列表中的任何地方添加整数。您从一个空的 List开始,然后检查它是否包含1这就是它转到 else 部分的原因。
You may initialize the list like:
您可以像这样初始化列表:
List<int> list = new List<int>() {1};
回答by maximpa
The first line initialisesan empty list:
第一行初始化一个空列表:
List<int> list = new List<int>();
The list is empty so Contains()returns false, no matter what the value you check for is.
该列表为空,因此Contains()返回 false,无论您检查的值是什么。
To initialise the list you could use:
要初始化列表,您可以使用:
List<int> list = new List<int> { 1 };
More details are here: Object and Collection Initializers (C# Programming Guide)
更多细节在这里: 对象和集合初始值设定项(C# 编程指南)
回答by ??c Bùi
I don't get ur code !
我没有得到你的代码!
First ur list have no element
首先你的列表没有元素
Your code , it may be like this:
你的代码,可能是这样的:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
int number = Convert.ToInt32(DobbelWaarde.Text);
if ( number == 1)
{
if (list.Contains(1))
{
Console.WriteLine(number + " is allready been chosen");
}
else
{
list.Add(number );
Console.WriteLine();
foreach (int li in list)
{
Console.WriteLine(li);
Console.WriteLine("We add " + number);
}
}
}
回答by kiran
condition in the if statement is always evaluated to false i.e list doesn't have 1 so it returns false hence else part gets executed.
if 语句中的条件总是被评估为假,即列表没有 1,所以它返回假,因此 else 部分被执行。
回答by Ria
Define your listouter checkTextBox method:
定义您的list外部 checkTextBox 方法:
List<int> list = new List<int>();
void CheckTextBox()
{
if (Convert.ToInt32(DobbelWaarde.Text) == 1)
{
if (list.Contains(1))
{
Console.WriteLine("1 is allready been chosen");
}
else
{
list.Add(1);
Console.WriteLine();
foreach (int li in list)
{
Console.WriteLine(li);
Console.WriteLine("We add 1");
}
}
}
}
回答by Tim Schmelter
You initialize the list always before you check if it contains that number. That will clear the list. So you should move the initialization for example into the constructor of your class:
您总是在检查列表是否包含该数字之前初始化列表。这将清除列表。因此,您应该将初始化例如移动到类的构造函数中:
class MyClass
{
List<int> list;
public MyClass()
{
list = new List<int>();
}
private void Validate()
{
if (Convert.ToInt32(DobbelWaarde.Text) == 1)
{
if (list.Contains(1))
{
Console.WriteLine("1 is allready been chosen");
}
else
{
list.Add(1);
// ...
}
}

