From 3c4123d51466b310eec68cdaf5758ead06cf0b3b Mon Sep 17 00:00:00 2001 From: Kister Hakusan <2753888203@qq.com> Date: Mon, 13 Oct 2025 19:38:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C#进阶/C#进阶.sln | 12 +++ C#进阶/Lesson7_List/Lesson7_List.csproj | 10 +++ C#进阶/Lesson7_List/Program.cs | 82 +++++++++++++++++++ .../Lesson7_List练习/Lesson7_List练习.csproj | 10 +++ C#进阶/Lesson7_List练习/Program.cs | 62 ++++++++++++++ 5 files changed, 176 insertions(+) create mode 100644 C#进阶/Lesson7_List/Lesson7_List.csproj create mode 100644 C#进阶/Lesson7_List/Program.cs create mode 100644 C#进阶/Lesson7_List练习/Lesson7_List练习.csproj create mode 100644 C#进阶/Lesson7_List练习/Program.cs diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln index 9d748da..7c6ef30 100644 --- a/C#进阶/C#进阶.sln +++ b/C#进阶/C#进阶.sln @@ -29,6 +29,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束", "Les EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束练习", "Lesson6_泛型约束练习\Lesson6_泛型约束练习.csproj", "{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List", "Lesson7_List\Lesson7_List.csproj", "{D63FDE7B-2A03-4A86-9E4F-223AF31F099B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson7_List练习", "Lesson7_List练习\Lesson7_List练习.csproj", "{1E65491F-EA06-4E4D-9E20-A692C9EB3C79}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -87,6 +91,14 @@ Global {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Debug|Any CPU.Build.0 = Debug|Any CPU {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.ActiveCfg = Release|Any CPU {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Release|Any CPU.Build.0 = Release|Any CPU + {D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D63FDE7B-2A03-4A86-9E4F-223AF31F099B}.Release|Any CPU.Build.0 = Release|Any CPU + {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Debug|Any CPU.Build.0 = Debug|Any CPU + {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.ActiveCfg = Release|Any CPU + {1E65491F-EA06-4E4D-9E20-A692C9EB3C79}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/C#进阶/Lesson7_List/Lesson7_List.csproj b/C#进阶/Lesson7_List/Lesson7_List.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson7_List/Lesson7_List.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson7_List/Program.cs b/C#进阶/Lesson7_List/Program.cs new file mode 100644 index 0000000..c8c3de4 --- /dev/null +++ b/C#进阶/Lesson7_List/Program.cs @@ -0,0 +1,82 @@ +namespace Lesson7_List +{ + internal class Program + { + static void Main(string[] args) + { + #region 知识点一 List的本质 + //List是C#为我们封装好的一个类 + //它的本质是一个可变类型的泛型数组 + //List类帮助我们实现了很多方法 + //比如泛型数组的增删查改 + #endregion + + #region 知识点二 声明 + //需要引用命名空间System.Collections.Generic + List list = new List(); + List list2 = new List(); + #endregion + + #region 知识点三 增删查改 + //增 + list.Add(1); + list.Add(2); + list.Add(3); + list2.Add("123"); + //范围增 + List list3 = new List(); + list3.Add("456"); + list2.AddRange(list3); + //插入 + list.Insert(0,9999); + + + //删 + //1.移除指定元素 + list.Remove(1); + //2.移除指定位置的元素 + list.RemoveAt(0); + //3.清空 + list.Clear(); + + //查 + //1.得到指定位置的元素 + Console.WriteLine(list[0]); + //2.查看元素是否存在 + if (list.Contains(1)) + { + Console.WriteLine("存在为1的元素"); + } + //3.正向查找元素位置 + //找到返回位置,找不到返回-1 + int index = list.IndexOf(1); + Console.WriteLine(index); + //4.反向查找元素位置 + //找到返回位置,找不到返回-1 + index = list.LastIndexOf(1); + Console.WriteLine(index); + + //改 + Console.WriteLine(list[0]); + list[0] = 99; + Console.WriteLine(list[0]); + #endregion + + #region 知识点四 遍历 + //长度 + Console.WriteLine(list.Count); + //容量 + //避免产生垃圾 + Console.WriteLine(list.Capacity); + for (int i = 0; i < list.Count; i++) + { + Console.Write(list[i]); + } + foreach (Object item in list) + { + Console.WriteLine(item); + } + #endregion + } + } +} diff --git a/C#进阶/Lesson7_List练习/Lesson7_List练习.csproj b/C#进阶/Lesson7_List练习/Lesson7_List练习.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/C#进阶/Lesson7_List练习/Lesson7_List练习.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/C#进阶/Lesson7_List练习/Program.cs b/C#进阶/Lesson7_List练习/Program.cs new file mode 100644 index 0000000..e9343e6 --- /dev/null +++ b/C#进阶/Lesson7_List练习/Program.cs @@ -0,0 +1,62 @@ +namespace Lesson7_List练习 +{ + #region 第二题 + abstract class Monster + { + public static List monsters = new List(); + public Monster() + { + monsters.Add(this); + } + + public abstract void Atk(); + } + class Boss: Monster + { + + public override void Atk() + { + Console.WriteLine("Boss攻击"); + } + } + class Goblin : Monster + { + + public override void Atk() + { + Console.WriteLine("哥布林攻击"); + } + } + #endregion + class Program + { + static void Main(string[] args) + { + #region 第一题 + List l1 = new List(); + for (int i = 10; i > 0; i--) + { + l1.Add(i); + } + l1.RemoveAt(4); + for (int i = 0; i < l1.Count; i++) + { + Console.WriteLine(l1[i]); + } + Console.WriteLine("_______________________________"); + #endregion + + #region 第二题 + Monster m1 = new Boss(); + Monster m2 = new Goblin(); + + Boss b = new Boss(); + Goblin g = new Goblin(); + for (int i = 0;i < Monster.monsters.Count;i++) + { + Monster.monsters[i].Atk(); + } + #endregion + } + } +}