读书人

C# 新手有关问题

发布时间: 2013-08-01 15:23:18 作者: rapoo

C# 新手问题
public partial class Form1 : Form
{
private ArrayList List = new ArrayList();


public Form1()
{
InitializeComponent();

List.Add("1");
List.Add("2");
List.Add("3");
List.Add("4");
List.Add("5");
List.Add("6");
List.Add("7");
List.Add("8");

}

private void button1_Click(object sender, EventArgs e)
{

}
}

新手 轻喷
我现在List中都是字符串 我想转化为int
这应该怎么做啊?
我要是点击botton1 有一个messagebox 把奇数 偶数分开...
新手 望大神们 帮我一下
[解决办法]
按照你的要求,我写了一个好理解的。

 private static ArrayList List = new ArrayList();

private void button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < List.Count; i++)
{
int value = Convert.ToInt32(List[i]);


if (value % 2 == 0)
{
//偶数
MessageBox.Show(value + "");
}
else
{
//奇数
}
}
}

private void Form1_Load(object sender, EventArgs e)
{
List.Add("1");
List.Add("2");
List.Add("3");
List.Add("4");
List.Add("5");
List.Add("6");
List.Add("7");
List.Add("8");
}


[解决办法]
修改一下

ArrayList List = new ArrayList();
List.Add("1");
List.Add("2");
List.Add("3");
List.Add("4");
List.Add("5");
List.Add("6");
List.Add("7");


List.Add("8");
var numlist = from string l in List
select Convert.ToInt32(l);
var numeven = numlist.Where(x => x % 2 == 0).ToList();
var numodd = numlist.Where(x => x % 2 != 0).ToList();

读书人网 >C#

热点推荐