C# list用法 C#中list用法实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C# list用法 C#中list用法实例

smartsmile2012   2021-03-22 我要评论
想了解C#中list用法实例的相关内容吗,smartsmile2012在本文为您仔细讲解C# list用法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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#程序设计有所帮助。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们