From 520285bae8b744955343b1077fe5fdeb2034bf4c Mon Sep 17 00:00:00 2001 From: gaoxin <793429844@qq.com> Date: Wed, 22 Dec 2021 15:12:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EC#=203.0=E7=89=B9=E6=80=A7=20?= =?UTF-8?q?=E7=9A=84=E9=83=A8=E5=88=86=EF=BC=88=E8=87=AA=E5=8A=A8=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E7=9A=84=E5=B1=9E=E6=80=A7,=E5=8C=BF=E5=90=8D?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B,=E6=9F=A5=E8=AF=A2=E8=A1=A8=E8=BE=BE?= =?UTF-8?q?=E5=BC=8F=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AnonymousType.json" | 7 +++ .../AnonymousType.md" | 48 ++++++++++++++++ .../AutoImplementedProperties.json" | 7 +++ .../AutoImplementedProperties.md" | 55 +++++++++++++++++++ .../QueryExpression.json" | 7 +++ .../QueryExpression.md" | 51 +++++++++++++++++ .../sample/Program.cs" | 43 +++++++++++++++ .../sample/sample.csproj" | 10 ++++ 8 files changed, 228 insertions(+) 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/AnonymousType.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/AnonymousType.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/AutoImplementedProperties.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/AutoImplementedProperties.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/QueryExpression.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/QueryExpression.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/sample/Program.cs" 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/sample/sample.csproj" 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.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/AnonymousType.json" new file mode 100644 index 0000000..3f3a94a --- /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/AnonymousType.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "AnonymousType.md", + "exercise_id": "47413f9f257545289bc30c177ce14d37", + "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/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" new file mode 100644 index 0000000..53fb0c6 --- /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/AnonymousType.md" @@ -0,0 +1,48 @@ +# C# 3.0 特性 匿名类型 + +匿名类型提供了一种方便的方法,可以将一组只读属性封装到单个对象中,而无需先显式定义类型。类型名称由编译器生成,在源代码级别不可用。每个属性的类型由编译器推断。 +您可以通过将new运算符与对象初始值设定项一起使用来创建匿名类型。 + +```csharp +var user = new { name = "Gao", id = 996, height = 172.5 }; // 此时编译器会提示匿名类型, 是 new {string name, int id, double height} +Console.WriteLine(string.Format("name {0}, id {1}, height {2}", user.name, user.id, user.height)); +``` + +如果一个类里面的属性和字段太多,想要用LINQ语句查找,可以试试匿名类型。以下代码举个例子,假如有一个examples是example类的集合,然后输出所有的Name,如果直接查询会把example类的所有属性和字段带过来,使用匿名类型会导致查询中返回的数据量较少。 + +```csharp +var result = from example in examples select new { example.Name }; + +foreach (var item in result) +{ + Console.WriteLine(item.Name); +} +``` + +在下列选项中,可以设置一个name是“csdn”的匿名类型的是: + +## 答案 + +``` +var csdn = new { name = "csdn" }; +``` + +## 选项 + +### A + +``` +var csdn = { name = "csdn" }; +``` + +### B + +``` +var csdn = new CSDN(){ name = "csdn" }; +``` + +### C + +``` +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.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/AutoImplementedProperties.json" new file mode 100644 index 0000000..569775d --- /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/AutoImplementedProperties.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "AutoImplementedProperties.md", + "exercise_id": "ce894b0d3ce14592a0af56e06a32854d", + "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/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" new file mode 100644 index 0000000..477d139 --- /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/AutoImplementedProperties.md" @@ -0,0 +1,55 @@ +# C# 3.0 特性 自动实现属性 + +在 C# 3.0 及更高版本,当属性访问器中不需要任何其他逻辑时,自动实现的属性会使属性声明更加简洁。 + +对于字段可以用属性来封装,上面代码的name就是用Name封装,并且使用时使用Name。C# 3.0特性支持了自动实现属性的访问器,这样就可以写成Id这样,也能实现一样的功能。这里的属性将set访问器声明为私有,那就是私有的访问级别,如果访问器只声明get访问器时,除了能在构造函数中可变,在其他任何位置都不可变。 + +```csharp +public class Example +{ + private string name; + + public string Name { get => name; set => name = value; } + + public string Id { get; set; } + + public int Count { get; private set; } + + public string UserId { get;} + + public Example(string userId) + { + this.UserId = userId; + } +} +``` + +如上代码,在下列选项中,不可以设置属性值的是: + +## 答案 + +``` +Example example = new Example(){UserId = Guid.NewGuid().ToString()}; +``` + +## 选项 + +### A + +``` +Example example = new Example(Guid.NewGuid().ToString()); +``` + +### B + +``` +Example example = new Example(); +example.Id = Guid.NewGuid().ToString(); +``` + +### C + +``` +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/QueryExpression.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/QueryExpression.json" new file mode 100644 index 0000000..a86c3ab --- /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/QueryExpression.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "Gao996", + "source": "QueryExpression.md", + "exercise_id": "20602ef8b2bf49cab1a5f3ff66523b10", + "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/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" new file mode 100644 index 0000000..b60bf82 --- /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/QueryExpression.md" @@ -0,0 +1,51 @@ +# C# 3.0 特性 查询表达式 + +查询表达式必须以from子句开头。 它指定数据源以及范围变量。查询表达式以select或group结尾。 + +* 使用 where 子句可基于一个或多个谓词表达式,从源数据中筛选出元素。 +* 使用 select 子句可生成所有其他类型的序列。 +* 使用 group 子句可生成按指定键组织的组的序列。 +* 使用 into 关键字可以在 select 或 group 子句中创建存储查询的临时标识符。 +* 使用 orderby 子句可按升序或降序对结果进行排序。 +* 使用 join 子句可基于每个元素中指定的键之间的相等比较,将一个数据源中的元素与另一个数据源中的元素进行关联和/或合并。 +* 使用 let 子句可将表达式(如方法调用)的结果存储在新范围变量中。 + +在下列选项中,可以将examples集合中所有Count大于10的子项目按照Count从大到小的顺序格式化显示的是: + +## 答案 + +``` +from example in examples +where example.Count > 10 +orderby example.Count descending +select $"{example.Name}\t{example.Count}"; +``` + +## 选项 + +### A + +``` +from example in examples +where example.Count > 10 +orderby example.Count ascending +select $"{example.Name}\t{example.Count}"; +``` + +### B + +``` +from example in examples +where example.Count > 10 +orderby example.Count +select $"{example.Name}\t{example.Count}"; +``` + +### C + +``` +from example in examples +where example.Count > 10 +orderby example.Name ascending +select $"{example.Name}\t{example.Count}"; +``` \ 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/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" new file mode 100644 index 0000000..9fc5694 --- /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/sample/Program.cs" @@ -0,0 +1,43 @@ +锘// 鑷姩瀹炵幇鐨勫睘鎬 +public class Example +{ + private string name; + + public string Name { get => name; set => name = value; } + + public string Id { get; set; } + + public int Count { get; private set; } + + public string UserId { get;} + + public Example(string userId) + { + this.UserId = userId; + } +} + + +// 鍖垮悕绫诲瀷 +var user = new { name = "Gao", id = 996, height = 172.5 }; // 姝ゆ椂缂栬瘧鍣ㄤ細鎻愮ず鍖垮悕绫诲瀷锛 鏄 new {string name, int id, double height} +Console.WriteLine(string.Format("name {0}, id {1}, height {2}", user.name, user.id, user.height)); + +var result = from example in examples select new { example.Name }; + +foreach (var item in result) +{ + Console.WriteLine(item.Name); +} + +// 鏌ヨ琛ㄨ揪寮 +from example in examples +where example.Count > 10 +orderby example.Count descending +select $"{example.Name}\t{example.Count}"; + +// Lambda 琛ㄨ揪寮 +// 琛ㄨ揪寮忔爲 +// 鎵╁睍鏂规硶 +// 闅愬紡绫诲瀷鏈湴鍙橀噺 +// 鍒嗛儴鏂规硶 +// 瀵硅薄鍜岄泦鍚堝垵濮嬪艰瀹氶」 \ 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/sample/sample.csproj" "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/sample.csproj" new file mode 100644 index 0000000..74abf5c --- /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/sample/sample.csproj" @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + -- GitLab