From c7d50f6b87b766d684cd235c3d3450ed09859525 Mon Sep 17 00:00:00 2001 From: gaoxin <793429844@qq.com> Date: Thu, 23 Dec 2021 14:58:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201..NET=E5=88=9D=E9=98=B6\3?= =?UTF-8?q?.C#=E7=89=B9=E6=80=A7\1.C#3.0=E7=89=B9=E6=80=A7=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E7=89=B9=E6=80=A7=20=E4=BF=AE=E6=94=B9=20=E9=83=A8?= =?UTF-8?q?=E5=88=86=E9=94=99=E5=88=AB=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnonymousType.md" | 8 +- .../AutoImplementedProperties.md" | 8 +- .../ExpressionTree.json" | 7 ++ .../ExpressionTree.md" | 48 ++++++++++ .../ExtensionMethod.json" | 7 ++ .../ExtensionMethod.md" | 88 +++++++++++++++++++ .../LambdaExpression.json" | 7 ++ .../LambdaExpression.md" | 42 +++++++++ .../QueryExpression.md" | 8 +- .../sample/Program.cs" | 24 +++++ .../WebAPI.md" | 2 +- 11 files changed, 236 insertions(+), 13 deletions(-) create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.json" create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.md" create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.json" create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.md" create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.json" create mode 100644 "data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.md" diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AnonymousType.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AnonymousType.md" index 53fb0c6..1118337 100644 --- "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AnonymousType.md" +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AnonymousType.md" @@ -23,7 +23,7 @@ foreach (var item in result) ## 答案 -``` +```csharp var csdn = new { name = "csdn" }; ``` @@ -31,18 +31,18 @@ var csdn = new { name = "csdn" }; ### A -``` +```csharp var csdn = { name = "csdn" }; ``` ### B -``` +```csharp var csdn = new CSDN(){ name = "csdn" }; ``` ### C -``` +```csharp var csdn = new() { name = "csdn" }; ``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AutoImplementedProperties.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AutoImplementedProperties.md" index 477d139..6baa579 100644 --- "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AutoImplementedProperties.md" +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/AutoImplementedProperties.md" @@ -28,7 +28,7 @@ public class Example ## 答案 -``` +```csharp Example example = new Example(){UserId = Guid.NewGuid().ToString()}; ``` @@ -36,20 +36,20 @@ Example example = new Example(){UserId = Guid.NewGuid().ToString()}; ### A -``` +```csharp Example example = new Example(Guid.NewGuid().ToString()); ``` ### B -``` +```csharp Example example = new Example(); example.Id = Guid.NewGuid().ToString(); ``` ### C -``` +```csharp Example example = new Example(); example.Name = Guid.NewGuid().ToString(); ``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.json" new file mode 100644 index 0000000..08f570b --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "ExpressionTree.md", + "exercise_id": "ea23a2f4010a4dd398b477d4990547a7", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.md" new file mode 100644 index 0000000..d64959c --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExpressionTree.md" @@ -0,0 +1,48 @@ +# C# 3.0 特性 表达式树 + +表达式树是定义代码的数据结构。 它们基于编译器用于分析代码和生成已编译输出的相同结构。 +以下代码创建了一个表达式并且执行了,Compile方法可以编译表达式树由描述为可执行代码的 lambda 表达式,并生成一个委托,表示 lambda 表达式。 +注意这里不能使用var来声明此表达式树,因为此操作无法执行,这是由于赋值右侧是隐式类型而导致的。 + +```csharp +Expression> add = () => 1 + 2; +var func = add.Compile(); // 创建一个 delegate +var answer = func(); // 调用 delegate, 得到值3 +Console.WriteLine(answer); +``` + +根据上面例子,下列选项中,可以生成一个1+2的表达式的是: + +## 答案 + +```csharp +var one = Expression.Constant(1, typeof(int)); +var two = Expression.Constant(2, typeof(int)); +var addition = Expression.Add(one, two); +``` + +## 选项 + +### A + +```csharp +var one = Expression.Constant(1); +var two = Expression.Constant(2); +var addition = Expression.Add(one, two); +``` + +### B + +```csharp +var one = (Expression)1; +var two = (Expression)2; +var addition = Expression.Add(one, two); +``` + +### C + +```csharp +var one = Expression.Constant(1, typeof(int)); +var two = Expression.Constant(2, typeof(int)); +var addition = one + two; +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.json" new file mode 100644 index 0000000..4dffe1e --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "ExtensionMethod.md", + "exercise_id": "2b4114f71e59455099471a65d2b0885f", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.md" new file mode 100644 index 0000000..0b3687a --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/ExtensionMethod.md" @@ -0,0 +1,88 @@ +# C# 3.0 特性 扩展方法 + +扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。 +扩展方法是一种静态方法,但可以像扩展类型上的实例方法一样进行调用。调用扩展方法与调用在类型中定义的方法没有明显区别。 + +在下列选项中,是扩展方法并实现了判断字符串中是否包含Q或q的代码的是: + +## 答案 + +```csharp +public static class Extensions +{ + public static bool IsContainQ(this String str) + { + foreach (var item in str) + { + if (item.Equals('Q') || item.Equals('q')) + { + return true; + } + } + + return false; + } +} +``` + +## 选项 + +### A + +```csharp +public class Extensions +{ + public bool IsContainQ(this String str) + { + foreach (var item in str) + { + if (item.Equals('Q') || item.Equals('q')) + { + return true; + } + } + + return false; + } +} +``` + +### B + +```csharp +public static class Extensions +{ + public static bool IsContainQ(this) + { + foreach (var item in str) + { + if (item.Equals('Q') || item.Equals('q')) + { + return true; + } + } + + return false; + } +} +``` + +### C + +```csharp +public static class Extensions +{ + public static bool IsContainQ(String str) + { + foreach (var item in str) + { + if (item.Equals('Q') || item.Equals('q')) + { + return true; + } + } + + return false; + } +} +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.json" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.json" new file mode 100644 index 0000000..b74715c --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "LambdaExpression.md", + "exercise_id": "99b3ef51bbb74e46b987729b18c9e311", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.md" new file mode 100644 index 0000000..ea0129e --- /dev/null +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/LambdaExpression.md" @@ -0,0 +1,42 @@ +# C# 3.0 特性 Lambda 表达式 + +Lambda表达式可采用表达式为其主体,也可以采用语句块作为其主体: + +```csharp +(input-parameters) => expression +(input-parameters) => { } +``` + +Lambda表达式可以没有参数,如果有参数需要在左侧添加,任何 Lambda 表达式都可以转换为委托类型。 + +在下列选项中,没有编译错误并且可以输出5的平方的是: + +## 答案 + +```csharp +Func square = x => x * x; +Console.WriteLine(square(5)); +``` + +## 选项 + +### A + +```csharp +Func square = () => x * x; +Console.WriteLine(square(5)); +``` + +### B + +```csharp +Func square = x * x; +Console.WriteLine(square(5)); +``` + +### C + +```csharp +Func square => x * x; +Console.WriteLine(square(5)); +``` \ No newline at end of file diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/QueryExpression.md" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/QueryExpression.md" index b60bf82..538426a 100644 --- "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/QueryExpression.md" +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/QueryExpression.md" @@ -14,7 +14,7 @@ ## 答案 -``` +```csharp from example in examples where example.Count > 10 orderby example.Count descending @@ -25,7 +25,7 @@ select $"{example.Name}\t{example.Count}"; ### A -``` +```csharp from example in examples where example.Count > 10 orderby example.Count ascending @@ -34,7 +34,7 @@ select $"{example.Name}\t{example.Count}"; ### B -``` +```csharp from example in examples where example.Count > 10 orderby example.Count @@ -43,7 +43,7 @@ select $"{example.Name}\t{example.Count}"; ### C -``` +```csharp from example in examples where example.Count > 10 orderby example.Name ascending diff --git "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" index 9fc5694..f4d1c02 100644 --- "a/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" +++ "b/data/1..NET\345\210\235\351\230\266/3.C#\347\211\271\346\200\247/1.C#3.0\347\211\271\346\200\247/sample/Program.cs" @@ -36,8 +36,32 @@ orderby example.Count descending select $"{example.Name}\t{example.Count}"; // Lambda 琛ㄨ揪寮 +Func square = x => x * x; +Console.WriteLine(square(5)); + // 琛ㄨ揪寮忔爲 +Expression> add = () => 1 + 2; +var func = add.Compile(); // 鍒涘缓涓涓 delegate +var answer = func(); // 璋冪敤 delegate锛 寰楀埌鍊3 +Console.WriteLine(answer); + // 鎵╁睍鏂规硶 +public static class Extensions +{ + public static bool IsContainQ(this String str) + { + foreach (var item in str) + { + if (item.Equals('Q') || item.Equals('q')) + { + return true; + } + } + + return false; + } +} + // 闅愬紡绫诲瀷鏈湴鍙橀噺 // 鍒嗛儴鏂规硶 // 瀵硅薄鍜岄泦鍚堝垵濮嬪艰瀹氶」 \ No newline at end of file diff --git "a/data/2..NET\344\270\255\351\230\266/1.ASP.NET Core\345\272\224\347\224\250/4.Web API\345\272\224\347\224\250/WebAPI.md" "b/data/2..NET\344\270\255\351\230\266/1.ASP.NET Core\345\272\224\347\224\250/4.Web API\345\272\224\347\224\250/WebAPI.md" index 71c934d..2645e0e 100644 --- "a/data/2..NET\344\270\255\351\230\266/1.ASP.NET Core\345\272\224\347\224\250/4.Web API\345\272\224\347\224\250/WebAPI.md" +++ "b/data/2..NET\344\270\255\351\230\266/1.ASP.NET Core\345\272\224\347\224\250/4.Web API\345\272\224\347\224\250/WebAPI.md" @@ -102,7 +102,7 @@ public class TestController : ControllerBase } ``` -如上代码,在下列选型中,可以正确获取Test接口的是: +如上代码,在下列选项中,可以正确获取Test接口的是: ## 答案 -- GitLab