protected void Page_Load(object sender, EventArgs e) { ListstudentNames = 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("
"); ListstudentScore = 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 Listlist = 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; }