C#中list用法实例

作者:smartsmile2012 时间:2022-07-02 13:57:00 

本文实例讲述了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#程序设计有所帮助。

标签:C#,list
0
投稿

猜你喜欢

  • C# Winform下载文件并显示进度条的实现代码

    2022-11-26 04:39:02
  • 浅谈Java线程并发知识点

    2021-10-20 13:11:13
  • 200行java代码实现2048小游戏

    2023-11-29 12:03:57
  • C# Winform实现进度条显示

    2023-09-14 15:47:51
  • Android Internet应用实现获取天气预报的示例代码

    2023-09-26 04:13:22
  • 仅用5分钟极速入门Dubbo使用教程

    2022-08-08 12:08:55
  • Java如何实现压缩文件与解压缩zip文件

    2022-01-28 09:14:00
  • Android系统自带分享图片功能

    2022-08-03 09:14:07
  • 基于springmvc之常用注解,操作传入参数

    2023-03-17 20:49:52
  • 浅谈java+内存分配及变量存储位置的区别

    2022-07-09 00:46:47
  • 使用监听器对Spring bean id进行唯一校验过程解析

    2022-04-12 18:07:21
  • Android中AutoCompleteTextView自动提示

    2022-06-21 19:07:36
  • Springboot使用redis实现接口Api限流的示例代码

    2023-11-29 02:11:05
  • Springboot使用@Valid 和AOP做参数校验及日志输出问题

    2023-12-05 04:39:12
  • Mybatis 级联删除的实现

    2022-05-08 21:18:09
  • MyBatis 中 ${}和 #{}的正确使用方法(千万不要乱用)

    2023-11-29 05:02:37
  • Android 中ScrollView与ListView冲突问题的解决办法

    2022-06-15 04:31:46
  • C#开发Winform实现文件操作案例

    2022-04-28 15:30:53
  • Java关系操作符简写介绍

    2023-12-25 12:56:29
  • springboot自定义Starter过程解析

    2023-07-24 22:24:55
  • asp之家 软件编程 m.aspxhome.com