C#基础:泛型列表数据转移、修改、遍历

一、数据转移和修改 

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
    static List<Student> stu = new List<Student>()
    {
        new Student(){ Id=1,Name="小明",Age=18,Score=100},
        new Student(){ Id=2,Name="小梅",Age=19,Score=80},
        new Student(){ Id=3,Name="小刘",Age=21,Score=50},
        new Student(){ Id=4,Name="小美",Age=20,Score=60}
    };

    static void Main(string[] args)
    {
        //目的:1.将stu的数据转移到tea  2.Title=学生姓名+“老师”
        List<Teacher> tea = new List<Teacher>();
        foreach (var student in stu)
        {
            Teacher teacher = new Teacher();
            teacher.Id = student.Id;
            teacher.Name = student.Name;
            teacher.Age = student.Age;
            teacher.Score = student.Score;
            teacher.Title = student.Name + "老师";
            tea.Add(teacher);
        }
    }
}

class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Score { get; set; }
}
class Teacher
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Score { get; set; }
    public string Title { get; set; }

}

二、遍历(含增加实体)

using System;
using System.Collections.Generic;
using System.Linq;

class MainClass
{
    static List<Student> stu = new List<Student>()
    {
        new Student(){ Id=1,Name="小明",Age=18,Score=100},
        new Student(){ Id=2,Name="小梅",Age=19,Score=80}
    };

    static void Main(string[] args)
    {
        foreach (var student in stu)
        {
            if (student.Id == 1)
            {
                student.value = new List<Value>(){
                    new Value(){ value=100 },
                    new Value(){ value=1000 }
                };
            }
            else if (student.Id == 2)
            {
                student.value = new List<Value>(){
                    new Value(){ value=200 },
                    new Value(){ value=2000 }
                };
            }
        }

        // 输出所有学生的信息
        foreach (var student in stu)
        {
            Console.WriteLine($"Id:{student.Id}, Name:{student.Name}, Score:{student.Score},Listvalue:{string.Join(',', student?.value.Select(x=>x.value))}");
        }
    }
}

class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public int Score { get; set; }
    public List<Value> value { get; set; }
}

class Value
{
    public int id { get; set; }
    public int value { get; set; }
}

三、参数任何值类型T的用法

public class ReturnResultDto<T>//初始化需要传入指定类型T
{
    public int RetCode { get; set; }
    public string Message { get; set; }
    public T ReturnData { get; set; }//T代表类型可以是任意的,若需列表可改成List<T>
}
public class Student
{
    public int id { get; set; }
    public string Name { get; set; }
}
public class Test
{
	static int Main(string[] args)
	{
        var test = new ReturnResultDto<Student>();
        test.RetCode = 500;
        test.Message = "success";
        test.ReturnData = new Student { id = 1, Name = "张三" };
        return 0;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值