From ecf5241ed6a88e5290697e98864c30475de3a18c Mon Sep 17 00:00:00 2001
From: Kister Hakusan <2753888203@qq.com>
Date: Mon, 13 Oct 2025 17:44:02 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Lesson6?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
C#进阶/C#进阶.sln | 12 ++
.../Lesson6_泛型约束/Lesson6_泛型约束.csproj | 10 +
C#进阶/Lesson6_泛型约束/Program.cs | 185 ++++++++++++++++++
.../Lesson6_泛型约束练习.csproj | 10 +
C#进阶/Lesson6_泛型约束练习/Program.cs | 182 +++++++++++++++++
5 files changed, 399 insertions(+)
create mode 100644 C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj
create mode 100644 C#进阶/Lesson6_泛型约束/Program.cs
create mode 100644 C#进阶/Lesson6_泛型约束练习/Lesson6_泛型约束练习.csproj
create mode 100644 C#进阶/Lesson6_泛型约束练习/Program.cs
diff --git a/C#进阶/C#进阶.sln b/C#进阶/C#进阶.sln
index 45feda1..9d748da 100644
--- a/C#进阶/C#进阶.sln
+++ b/C#进阶/C#进阶.sln
@@ -25,6 +25,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型", "Lesson5_
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson5_泛型练习", "Lesson5_泛型练习\Lesson5_泛型练习.csproj", "{524E1BC4-FE9A-4186-B7EE-A04323E98696}"
EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束", "Lesson6_泛型约束\Lesson6_泛型约束.csproj", "{79ACEF40-536F-48BC-B53F-92FEBD479704}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lesson6_泛型约束练习", "Lesson6_泛型约束练习\Lesson6_泛型约束练习.csproj", "{3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}"
+EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@@ -75,6 +79,14 @@ Global
{524E1BC4-FE9A-4186-B7EE-A04323E98696}.Debug|Any CPU.Build.0 = Debug|Any CPU
{524E1BC4-FE9A-4186-B7EE-A04323E98696}.Release|Any CPU.ActiveCfg = Release|Any CPU
{524E1BC4-FE9A-4186-B7EE-A04323E98696}.Release|Any CPU.Build.0 = Release|Any CPU
+ {79ACEF40-536F-48BC-B53F-92FEBD479704}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {79ACEF40-536F-48BC-B53F-92FEBD479704}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {79ACEF40-536F-48BC-B53F-92FEBD479704}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {79ACEF40-536F-48BC-B53F-92FEBD479704}.Release|Any CPU.Build.0 = Release|Any CPU
+ {3BE8ABBA-44EA-4112-B88B-220C71FBFFC8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
diff --git a/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj b/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj
new file mode 100644
index 0000000..2150e37
--- /dev/null
+++ b/C#进阶/Lesson6_泛型约束/Lesson6_泛型约束.csproj
@@ -0,0 +1,10 @@
+
+
+
+ Exe
+ net8.0
+ enable
+ enable
+
+
+
diff --git a/C#进阶/Lesson6_泛型约束/Program.cs b/C#进阶/Lesson6_泛型约束/Program.cs
new file mode 100644
index 0000000..e487c19
--- /dev/null
+++ b/C#进阶/Lesson6_泛型约束/Program.cs
@@ -0,0 +1,185 @@
+namespace Lesson6_泛型约束
+{
+ #region 知识回顾
+ //泛型类
+ class TestClass
+ {
+ public T t;
+ public U u;
+
+ public U TestFun(T t)
+ {
+ return default(U);
+ }
+
+ //泛型函数
+ public V TestFun(K k)
+ {
+ return default(V);
+ }
+ }
+ #endregion
+
+ #region 知识点一 什么是泛型约束
+ //让泛型的类型有一定限制
+ //关键字:where
+ //泛型约束一共有6种
+ //1.值类型 where 泛型字母:struct
+ //2.引用类型 where 泛型字母:class
+ //3.存在无参公共构造函数 where 泛型字母:new()
+ //4.某个类本身或其派生类 where 泛型字母:类名
+ //5.某个接口的派生类 where 泛型字母:接口名
+ //6.另一个泛型类本身或派生类 where 泛型字母:另一个泛型字母
+ #endregion
+
+ #region 知识点二 各泛型约束讲解
+ class Test1 where T : struct
+ {
+ public T v;
+
+ public void TestFun(K v) where K : struct
+ {
+
+ }
+ }
+
+ class Test2 where T : class
+ {
+ public T value;
+ public void TestFun(K v) where K : class
+ {
+
+ }
+ }
+
+
+ class Test3 where T: new()
+ {
+ public T value;
+ public void TestFun(K v) where K : new()
+ {
+
+ }
+ }
+ class Test1
+ {
+
+ }
+ class Test2
+ {
+ public Test2(int i)
+ {
+
+ }
+ }
+
+
+ class Test4 where T : Test1
+ {
+ public T value;
+ public void TestFun(K v) where K : Test1
+ {
+
+ }
+ }
+ class Test3 : Test1
+ {
+
+ }
+
+
+ interface IFly
+ {
+
+ }
+
+ class Test4 : IFly
+ {
+
+ }
+ class Test5 where T : IFly
+ {
+ public T value;
+ public void TestFun(K v) where K : IFly
+ {
+
+ }
+ }
+
+
+ class Test6 where T : U
+ {
+ public T value;
+ public void TestFun(K k) where K : V
+ {
+
+ }
+ }
+ #endregion
+
+ #region 知识点三 约束的组合使用
+ class Test7 where T : class, new()
+ {
+
+ }
+ #endregion
+
+ #region 知识点四 多个泛型有约束
+ class Test8 where T:class,new() where K : struct
+ {
+
+ }
+ #endregion
+
+ #region 总结
+ //泛型约束: 让类型有一定限制
+ //class
+ //struct
+ //new()
+ //类名
+ //接口名
+ //另一个泛型字母
+
+ //注意:
+ //1.可以组合使用
+ //2.多个泛型约束 用where连接即可
+ #endregion
+ internal class Program
+ {
+ static void Main(string[] args)
+ {
+ TestClass t = new TestClass();
+ t.t = "123fgd";
+ t.u = 10;
+
+ t.TestFun("123");
+ t.TestFun(123);
+
+
+ //Test1