本文實(shí)例講述了C#中l(wèi)ist用法。分享給大家供大家參考,具體如下:
protected void Page_Load(object sender, EventArgs e){  List<string> studentNames = new List<string>();  studentNames.Add("John");  studentNames.Add("Mary");  studentNames.Add("Rose");  //顯示各元素  foreach (string item in studentNames)  {    Response.Write(item);    Response.Write("<br/>");  }  Response.Write("<br/><br/>");  //List轉(zhuǎn)換成符號(hào)分隔字符串  string studentAllName = string.Join(",", studentNames.ToArray());  Response.Write(studentAllName);  Response.Write("<br/><br/>");  List<decimal> studentScore = new List<decimal>();  studentScore.Add(100);  studentScore.Add(98);  studentScore.Add(59);  //排序  studentScore.Sort();  //反轉(zhuǎn)排序  studentScore.Reverse();  //顯示各元素  foreach (decimal score in studentScore)  {    Response.Write(score);    Response.Write("<br/>");  }  //總計(jì)SUM  Response.Write("總分" + studentScore.Sum());  Response.Write("<br/>");  //List中是否存在  Response.Write(studentScore.Exists(MatchPRE));  Response.Write("<br/><br/>");  //List轉(zhuǎn)換成JSon  List<Student> list = new List<Student>();  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("<br/><br/>");}private static bool MatchPRE(decimal p)//條件匹配函數(shù),list1中每個(gè)元素都會(huì)傳入P中                                      //匹配后函數(shù)返回{  if (p == 100)//此句為匹配條件,如果匹配,返回,你可以隨意更改成你想要的值    return true;  else  {    return false;  }}public struct Student{  public string Name;  public int Age;  public string Sex;}新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注