C#中list用法實例。本站提示廣大學習愛好者:(C#中list用法實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中list用法實例正文
本文實例講述了C#中list用法。分享給年夜家供年夜家參考,詳細以下:
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轉換成符號分隔字符串
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();
//反轉排序
studentScore.Reverse();
//顯示各元素
foreach (decimal score in studentScore)
{
Response.Write(score);
Response.Write("<br/>");
}
//總計SUM
Response.Write("總分" + studentScore.Sum());
Response.Write("<br/>");
//List中能否存在
Response.Write(studentScore.Exists(MatchPRE));
Response.Write("<br/><br/>");
//List轉換成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)//前提婚配函數,list1中每一個元素都邑傳入P中 //婚配後函數前往
{
if (p == 100)//此句為婚配前提,假如婚配,前往,你可以隨便更改成你想要的值
return true;
else
{
return false;
}
}
public struct Student
{
public string Name;
public int Age;
public string Sex;
}
願望本文所述對年夜家C#法式設計有所贊助。