首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 新手有关问题

2013-08-01 
C# 新手问题public partial class Form1 : Form{private ArrayList List new ArrayList()public Form1(

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();

热点排行