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 53fb0c656ab640dcb7cb3c55f56b1f6cb2439476..1118337ed06a7495254865eb609af0421879169c 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 477d1396e02324ef6e188bf479e3e11ba475e035..6baa579b3ef510e7a0afa39ae20adcf909ebc505 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 0000000000000000000000000000000000000000..08f570bafccb2e3a60c63968c9b30bb892e063a5 --- /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 0000000000000000000000000000000000000000..d64959c6a8ccf23ecb1cff669611f4b95ee5e2a2 --- /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 0000000000000000000000000000000000000000..4dffe1e7327a43a86ba4568b50c56c39e24ce202 --- /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 0000000000000000000000000000000000000000..0b3687a58bdc4debe8500a9a7281ce86fd8692b9 --- /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 0000000000000000000000000000000000000000..b74715c8b1acdf5fcdf31bbf64dc448b4d94c72d --- /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 0000000000000000000000000000000000000000..ea0129e35e7b9a84013ac4ff4df32506aa13e5de --- /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 b60bf82e7f73b187840f14d707c20d3b50c9fbb5..538426a2160c9c876ac944de62d1c23dbc251fc5 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 9fc5694196e95569d0ffb2cb9f6e975600f10200..f4d1c02c9a187e9f1ecd45858cafcc693b637535 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 71c934dbd090b131af6edf930a3a5e8c0924e57d..2645e0ef784b8d5b5ac2be9dabad032dca587371 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接口的是: ## 答案