List<T>属性和方法使用

cs 复制代码
//@author:shark_ddd
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//使用函数来减少长度
 namespace List_T
{
    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    internal class Program
    {
        static void PrintStudents(List<Student> students)
        {
            foreach (Student student in students)
            {
                Console.Write("Name:" + student.Name);
                Console.WriteLine(", Age:" + student.Age);
            }
        }

        static void PrintNumbers(List<int> numbers)
        {
            foreach (int number in numbers)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();
        }

        static void PrintStrings(List<string> strings)
        {
            foreach (string str in strings)
            {
                Console.Write(" " + str);
            }
            Console.WriteLine();
        }

        static void Main(string[] args)
        {
            List<Student> studentList = new List<Student>
            {
                new Student { Name = "Alice", Age = 20 },
                new Student { Name = "Ben", Age = 18 },
                new Student { Name = "Clark", Age = 18 }
            };

            Console.WriteLine("Students:");
            PrintStudents(studentList);

            Console.WriteLine("=================================================================================");

            // 创建数字列表
            List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };

            // 在数字列表末尾加入 6
            numberList.Add(6);

            // 在数字列表末尾加入 7、8、9
            numberList.AddRange(new List<int> { 7, 8, 9 });

            Console.WriteLine("now_List:");
            PrintNumbers(numberList);

            // 获取数字列表中的所有偶数
            List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);
            Console.WriteLine("获取的偶数有:");
            PrintNumbers(evenNumbers);

            // 删除数字列表中的 9
            numberList.Remove(9);
            Console.WriteLine("now_List:");
            PrintNumbers(numberList);

            // 删除数字列表中索引为 0 的元素
            numberList.RemoveAt(0);
            Console.WriteLine("now_List:");
            PrintNumbers(numberList);

            // 打印第一个
            Console.WriteLine("First_number:" + numberList.First());

            Console.WriteLine("=================================================================================");

            // 水果列表
            List<string> fruitList = new List<string> { "apple", "banana", "cherry" };

            // 在索引为 1 的位置插入 orange
            fruitList.Insert(1, "orange");
            Console.WriteLine("now_Fruit:");
            PrintStrings(fruitList);

            // 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入
            string[] myFruit = new string[2];
            fruitList.CopyTo(0, myFruit, 0, 2);
            Console.WriteLine("myFruit:");
            Console.WriteLine(string.Join(" ", myFruit));

            // 复制水果列表从索引 1 开始的三个元素到 subList 中
            List<string> subList = fruitList.GetRange(1, 3);
            Console.WriteLine("subList:");
            PrintStrings(subList);

            // 翻转水果列表元素排序顺序
            fruitList.Reverse();
            Console.WriteLine("now_Fruit:");
            PrintStrings(fruitList);

            // 对水果列表元素进行排序
            fruitList.Sort();
            Console.WriteLine("now_Fruit:");
            PrintStrings(fruitList);

            // 清空水果列表
            fruitList.Clear();
            Console.WriteLine("now_Fruit:");
            PrintStrings(fruitList);

            Console.ReadLine();
        }
    }
}


//转换成字符串string.Join
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using System.Threading.Tasks;

//namespace List_T
//{
//    class Student
//    {
//        public string Name { get; set; }
//        public int Age { get; set; }
//    }

//    internal class Program
//    {
//        static void Main(string[] args)
//        {
//            List<Student> studentList = new List<Student>
//            {
//                new Student { Name = "Alice", Age = 20 },
//                new Student { Name = "Ben", Age = 18 },
//                new Student { Name = "Clark", Age = 18 }
//            };

//            Console.WriteLine("Students: " + string.Join(", ", studentList.Select(s => $"Name:{s.Name}, Age:{s.Age}")));

//            Console.WriteLine("=================================================================================");

//            // 创建数字列表
//            List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };

//            // 在数字列表末尾加入 6
//            numberList.Add(6);

//            // 在数字列表末尾加入 7、8、9
//            numberList.AddRange(new List<int> { 7, 8, 9 });

//            Console.WriteLine("now_List: " + string.Join(" ", numberList));

//            // 获取数字列表中的所有偶数
//            List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);
//            Console.WriteLine("获取的偶数有: " + string.Join(" ", evenNumbers));

//            // 删除数字列表中的 9
//            numberList.Remove(9);
//            Console.WriteLine("now_List: " + string.Join(" ", numberList));

//            // 删除数字列表中索引为 0 的元素
//            numberList.RemoveAt(0);
//            Console.WriteLine("now_List: " + string.Join(" ", numberList));

//            // 打印第一个
//            Console.WriteLine("First_number:" + numberList.First());

//            Console.WriteLine("=================================================================================");

//            // 水果列表
//            List<string> fruitList = new List<string> { "apple", "banana", "cherry" };

//            // 在索引为 1 的位置插入 orange
//            fruitList.Insert(1, "orange");
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));

//            // 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入
//            string[] myFruit = new string[2];
//            fruitList.CopyTo(0, myFruit, 0, 2);
//            Console.WriteLine("myFruit: " + string.Join(" ", myFruit));

//            // 复制水果列表从索引 1 开始的三个元素到 subList 中
//            List<string> subList = fruitList.GetRange(1, 3);
//            Console.WriteLine("subList: " + string.Join(" ", subList));

//            // 翻转水果列表元素排序顺序
//            fruitList.Reverse();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));

//            // 对水果列表元素进行排序
//            fruitList.Sort();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));

//            // 清空水果列表
//            fruitList.Clear();
//            Console.WriteLine("now_Fruit: " + string.Join(" ", fruitList));

//            Console.ReadLine();
//        }
//    }
//}    

/*冗长的foreach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace List_T
{

    class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    internal class Program
    {

        static void Main(string[] args)
        {
            List<Student> studentList = new List<Student>
        {
            new Student { Name = "Alice", Age = 20 },
            new Student { Name = "Ben", Age = 18 },
            new Student { Name = "Clark", Age = 18 }
        };
            foreach (Student student in studentList)
            {
                Console.Write("Name:" + student.Name);
                Console.WriteLine(",  Age:" + student.Age);
            }

            Console.WriteLine("=================================================================================");
            // 创建数字列表
            List<int> numberList = new List<int> { 1, 2, 3, 4, 5 };

            // 在数字列表末尾加入 6
            numberList.Add(6);

            // 在数字列表末尾加入 7、8、9
            numberList.AddRange(new List<int> { 7, 8, 9 });

            Console.Write("now_List:");
            foreach (int number in numberList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 获取数字列表中的所有偶数
            List<int> evenNumbers = numberList.FindAll(n => n % 2 == 0);
            Console.Write("获取的偶数有:");
            foreach (int number in evenNumbers)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 删除数字列表中的 9
            numberList.Remove(9);
            Console.Write("now_List:");
            foreach (int number in numberList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 删除数字列表中索引为 0 的元素
            numberList.RemoveAt(0);
            Console.Write("now_List:");
            foreach (int number in numberList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            //打印第一个
            Console.WriteLine("First_number:" + numberList.First());

            Console.WriteLine("=================================================================================");

            // 水果列表
            List<string> fruitList = new List<string> { "apple", "banana", "cherry" };

            // 在索引为 1 的位置插入 orange
            fruitList.Insert(1, "orange");
            Console.Write("now_Fruit:");
            foreach (string number in fruitList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 复制水果列表从索引 0 开始的两个元素到 myFruit 数组中,并从数组的索引 0 开始存入
            string[] myFruit = new string[2];
            fruitList.CopyTo(0, myFruit, 0, 2);
            Console.Write("myFruit:");
            foreach (string number in myFruit)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 复制水果列表从索引 1 开始的三个元素到 subList 中
            List<string> subList = fruitList.GetRange(1, 3);
            Console.Write("subList:");
            foreach (string number in subList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 翻转水果列表元素排序顺序
            fruitList.Reverse();
            Console.Write("now_Fruit:");
            foreach (string number in fruitList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            // 对水果列表元素进行排序
            fruitList.Sort();
            Console.Write("now_Fruit:");
            foreach (string number in fruitList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();
            // 清空水果列表
            fruitList.Clear();
            Console.Write("now_Fruit:");
            foreach (string number in fruitList)
            {
                Console.Write(" " + number);
            }
            Console.WriteLine();

            Console.ReadLine();
        }
    }
}*/
相关推荐
2501_9159090610 分钟前
Windows 上用 VS Code 调试 iPhone 真机 App,KXBuild 断点、崩溃定位实操
ide·windows·vscode·ios·iphone·个人开发·敏捷流程
情 九39 分钟前
MCP 开发环境搭建:Mac 与 Windows 跑通 Python 服务和 stdio 客户端
windows·python·macos·mcp
海盗12341 小时前
微软技术日报 2026-10-29:Copilot harness 进 Foundry,云重建能拉镜像
人工智能·microsoft·机器人·aigc·copilot
畅想视界ThinkView2 小时前
病区护士站触控终端选型指南:分诊叫号与护理交互终端怎么挑
前端·microsoft·交互
懂点Tech10 小时前
ctfshow misc28:GIF 高度修改解题与脚本实现
开发语言·windows·python
云梦谭12 小时前
让 Linux 主动连接 Windows:配置 Windows 11 SSH 服务端
linux·windows·ssh
奇树谦12 小时前
Windows CMD 下 mc 运维指南:SeaweedFS 安装验收与 MinIO 数据迁移
运维·windows
keren216 小时前
SceneDock:Windows 场景化工作台——一键启动工作链,还能被 AI Agent 调用(CLI / MCP)
windows·开发工具·效率工具·mcp
成旭先生18 小时前
车辆、能源与地理数据 API 实战:把定价、油价、行政区划串成一条链路
人工智能·windows·能源·api集成·油价查询·车辆定价·地址清洗
seahahacsdn20 小时前
Windows获取SYSTEM用户权限的方法应用-计划任务
windows