110,568
社区成员
发帖
与我相关
我的任务
分享
List<string> ListArray = new List<string>();
string str ="";
//生成str 并开始对ListArray进行填充
if(!ListArray.Contains(str)) //检查str 是否是ListArray中的项
{
//填充
}
-----------------------------------------------------
无可厚非的,如果ListArray中存在的一个元素与str相同,str不添加到ListArray中。但是,洽洽问题出来了!
fun(str)
{
lock(obj)
{
if(!ListArray.Contains(str)) //这里无法正常判断,即str是ListArray中的项,也返回false
//填充
else
//不填充
}
}
在这个函数里即使str是ListArray中的项,也返回false, 假使我们在做一个按钮,在按钮单击事件下再对ListArray使用ListArray.Contains 却能正常检查到是否存在相同项?
会是什么原因呢?
private void addtoarray(string addstr)
{
lock (thisLock)
{
for (int i = 0; i < listarray.Count; i++)
{
if (listarray[i].Contains(addstr))
{
string show = addstr + " has been add to array";
new ToolTip().Show(show, textBox1, 800);
}
if (i==listarray.Count-1)
{
listarray.Add(addstr);
}
}
}
}
namespace ListArrayContains
{
public partial class Form1 : Form
{
private Object thisLock = new Object();
List<string> listarray = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string addstr = textBox1.Text;
addtoarray(addstr);
}
private void addtoarray(string addstr)
{
lock (thisLock)
{
if (!listarray.Contains(addstr))
{
listarray.Add(addstr);
}
else
{
string show = addstr + " has been add to array";
new ToolTip().Show(show, textBox1, 800);
}
}
}
}
}
List<string> ListArray = new List<string>();
string str = "1";
ListArray.Add("1");
ListArray.Add("2");
if (ListArray.Contains(str))
{
}
public partial class Form1 : Form
{
private Object thisLock = new Object();
List<string> listarray = new List<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
lock(thisLock)
{
if (!listarray.Contains(textBox1.Text))
{
listarray.Add(textBox1.Text);
}
else
{
new ToolTip().Show("did add to array", textBox1,500);
}
}
}
}
List<string> ListArray = new List<string>();
ListArray.Add("qw");
bool aa= ListArray.Contains("qw "); //false
bool bb = ListArray.Contains("qw"); //true
public partial class Form1 : Form