程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#中list使用示例

c#中list使用示例

編輯:C#入門知識

    protected void Page_Load(object sender, EventArgs e)
    {
        List studentNames = new List();
        studentNames.Add("John");
        studentNames.Add("Mary");
        studentNames.Add("Rose");

        //顯示各元素
        foreach (string item in studentNames)
        {
            Response.Write(item);
            Response.Write("
"); } Response.Write("

"); //List轉換成符號分隔字符串 string studentAllName = string.Join(",", studentNames.ToArray()); Response.Write(studentAllName); Response.Write("

"); List studentScore = new List(); studentScore.Add(100); studentScore.Add(98); studentScore.Add(59); //排序 studentScore.Sort(); //反轉排序 studentScore.Reverse(); //顯示各元素 foreach (decimal score in studentScore) { Response.Write(score); Response.Write("
"); } //總計SUM Response.Write("總分" + studentScore.Sum()); Response.Write("
"); //List中是否存在 Response.Write(studentScore.Exists(MatchPRE)); Response.Write("

"); //List轉換成JSon List list = new List(); for (int i = 0; i < 5; i++) { Student a = new Student(); a.Name = "張三" + i; a.Age = i; a.Sex = "男"; list.Add(a); } string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list); Response.Write(json); Response.Write("

"); } private static bool MatchPRE(decimal p)//條件匹配函數,list1中每個元素都會傳入P中 //匹配後函數返回 { if (p == 100)//此句為匹配條件,如果匹配,返回,你可以隨意更改成你想要的值 return true; else { return false; } } public struct Student { public string Name; public int Age; public string Sex; }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved