c#學習

c#中所有的類型都來自object
int i = 9;
//object o = 4;
//o=new Student();//裝箱
// int i=Convert.ToInt32(o);//出箱

static void Main(string[] args)
{
int[] arr = new int[5] { 1, 2, 3, 4, 5 };
Console.WriteLine(arr.Average().ToString());
Console.WriteLine(arr.Length.ToString());
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i] + “\t”);
}

        object[] arrobject = new object[3];
        arrobject[0] = 3;
        arrobject[1] = 4.4;
        arrobject[2] = new User();
        User u1 = (User)arrobject[2];
        User[] users = new User[2];
        users[0] = new User();
        users[0].Name = "hell";
        users[1] = new User();
        users[1].Name = "llo";
        foreach (User u in users)
        {
            Console.WriteLine("student's name:{0}", u.Name);
        }
        //冒泡排序
        int[] scores = new int[5] { 12, 43, 52, 563, 21 };
        for (int i = 0; i < scores.Length - 1; i++)
            for (int j = 0; j < scores.Length - 1 - i; j++)
            {
                int tmp;
                if (scores[j] > scores[j + 1])
                {
                    tmp = scores[j];
                    scores[j] = scores[j + 1];
                    scores[j + 1] = tmp;
                }
            }
        //foreach循環
        foreach (int i in scores)
        {
            Console.WriteLine(i);
        }
        Email email = new Email();
        string choice = "";
        do
        {
            email.PickName();
            choice = Console.ReadLine();
            choice = choice.ToLower();//轉爲小寫
            Console.WriteLine("input y or n");
        }
        while (choice.Equals("y"));

        Console.WriteLine("input message and split by \n");//分割字符串
        string s = Console.ReadLine();
        string[] arrstring;
        arrstring = s.Split(' ');
        foreach (string str in arrstring)
        {
            Console.WriteLine(str);
        }

        //
        ArrayList arrlist = new ArrayList();
        arrlist.Add(2);
        arrlist.Add(0);
        arrlist.Add(new User());
        int it = (int)arrlist[0];
        arrlist.Insert(0, 20);//在索引處插入
        arrlist.RemoveAt(0);//通過索引刪除

        User u11 = new User();
        u11.Account = "aaaaa";
        u11.Banlance = 34.1;
        User u2 = new User();
        u2.Account = "bbbb";
        u2.Banlance = 4.4;


        Hashtable ht = new Hashtable();//hashtable 鍵值對類比於java map
        ht.Add(u11.Account, u11);
        ht.Add(u2.Account, u2);
        foreach (object obj in ht.Keys)
        {
            Console.WriteLine(obj.ToString());
        }
        foreach (object obj in ht.Values)
        {
            User u = (User)obj;
            Console.WriteLine(u.Account);
        }

        //另一種遍歷
        foreach (DictionaryEntry en in ht)
        {
            Console.WriteLine("\t{0}", en.Key);
            Console.WriteLine("\t{0}", ((User)en.Value).Banlance);
        }


        Dictionary<int, string> d = new Dictionary<int, string>();//類型安全的,與hashtable對應
        d.Add(1, "hell");
        d.Add(2, "llo");

    }
發佈了43 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章