C#中list用法实例
本文实例讲述了C#中list用法。分享给大家供大家参考,具体如下:
protectedvoidPage_Load(objectsender,EventArgse)
{
List<string>studentNames=newList<string>();
studentNames.Add("John");
studentNames.Add("Mary");
studentNames.Add("Rose");
//显示各元素
foreach(stringiteminstudentNames)
{
Response.Write(item);
Response.Write("<br/>");
}
Response.Write("<br/><br/>");
//List转换成符号分隔字符串
stringstudentAllName=string.Join(",",studentNames.ToArray());
Response.Write(studentAllName);
Response.Write("<br/><br/>");
List<decimal>studentScore=newList<decimal>();
studentScore.Add(100);
studentScore.Add(98);
studentScore.Add(59);
//排序
studentScore.Sort();
//反转排序
studentScore.Reverse();
//显示各元素
foreach(decimalscoreinstudentScore)
{
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=newList<Student>();
for(inti=0;i<5;i++)
{
Studenta=newStudent();
a.Name="张三"+i;
a.Age=i;
a.Sex="男";
list.Add(a);
}
stringjson=newSystem.Web.Script.Serialization.JavaScriptSerializer().Serialize(list);
Response.Write(json);
Response.Write("<br/><br/>");
}
privatestaticboolMatchPRE(decimalp)//条件匹配函数,list1中每个元素都会传入P中//匹配后函数返回
{
if(p==100)//此句为匹配条件,如果匹配,返回,你可以随意更改成你想要的值
returntrue;
else
{
returnfalse;
}
}
publicstructStudent
{
publicstringName;
publicintAge;
publicstringSex;
}
更多关于C#相关内容感兴趣的读者可查看本站专题:《C#程序设计之线程使用技巧总结》、《C#操作Excel技巧总结》、《C#中XML文件操作技巧汇总》、《C#常见控件用法教程》、《WinForm控件用法总结》、《C#数据结构与算法教程》、《C#数组操作技巧总结》及《C#面向对象程序设计入门教程》
希望本文所述对大家C#程序设计有所帮助。