所以基本上我有这个课程:
class Postnumre
{
public static int[] PostnumberWest =
{
5320,6753,6534,8961,6051,8592,8643,6823,9510,5466,5610,9340,6440,7490,8963,5935,8444,7150,6210,8330,7755,6541,6852,7190,9881,8850,6091,5603,9492,5491,6857,5400,6392,7441,9998,8220,6740,7330,6535,6261,7182,5464,6310,5672,9460,8654,8740,9700,6650,6372,6622,7660,9574,7080,7650,6070,5380,8721,9330,9352,5631,8400,6320,6040,8250,5592,7361,7442,7950,6700,6701,6715,6710,6705,7997,9997,7996,9996,6720,9640,5863,9690,8762,7000,7029,9900,5871,7741,7884,6683,5600,8990,8882,7321,8464,9362,9631,8751,5591,6621,5854,9260,7323,8983,8883,5620,6752,8585,6510,6771,8500,7200,6300,5892,5884,6690,6100,7540,8370,9560,9370,8450,7362,7730,7673,8462,5463,8361,8970,8722,6094,7250,6893,6854,7400,7429,5874,8382,9850,9320,7560,8530,9800,9500,7500,6670,8543,8783,8700,6682
};
}
从一个按钮我想检查它是否包含这样的给定值:
private void radButton6_Click(object sender, EventArgs e)
{
if(Postnumre.PostnumberEast.Contains("5320"))
{
}
}
提前致谢!
答案 0 :(得分:1)
数组包含int
s,而不是string
s。只需删除引号就可以了:
// West, not East, though I'll give you the benefit of the doubt that your Postnumre class how both members
if(Postnumre.PostnumberEast.Contains(5320))
{
//...
}
这有效(假设你有必要的using
指令)因为数组实现IEnumerable<T>
,而Contains()
是该接口上的扩展方法。你也可以这样做:
if(Array.IndexOf(Postnumre.PostnumberEast, 5320) > -1)
{
//...
}
还有一个选择是使用HashSet<int>
而不是数组。然后,您可以再次使用Contains()
方法进行检查,只要您的集合大于10个项目,性能就会更好。缺点是初始化语法不是那么干净。
答案 1 :(得分:0)
private void radButton6_Click(object sender, EventArgs e)
{
// lose the quotes, its a int array not a string array
// also the field name is PostnumberWest not PostnumberEast
// unless you have a field called PostnumberEast
if(Postnumre.PostnumberWest .Contains(5320))
{
}
}
答案 2 :(得分:-1)
实际上有一种方法,但您必须在旁边留下引号,以便输入整数。