提交 920e824c 编写于 作者: S sangsq

增加 C# 6.0

上级 84b11c70
{
"type": "code_options",
"author": "marin1993",
"source": "AutoPropertyInitializers.md",
"notebook_enable": false,
"exercise_id": "70f3d6f8c0414f62af71f833a619488f"
}
\ No newline at end of file
# 自动属性初始化表达式
自动属性初始化表达式允许直接在属性的声明内分配属性。对于只读属性,它负责确保属性固定不变所需的所有繁琐程序。
例如,请看示例中的 FingerPrint 类:
```csharp
public class FingerPrint
{
public DateTime TimeStamp { get; } = DateTime.UtcNow;
public string Process { get; } =
System.Diagnostics.Process.GetCurrentProcess().ProcessName;
}
```
以下自动属性初始化表达式使用错误的是:
## 答案
```csharp
public string WebSite { get; set = "csdn"; };
```
## 选项
### A
```csharp
public string FirstName { get; set; } = string.Empty;
```
### B
```csharp
public string FirstName { get; init; } = "string.Empty";
```
### C
```csharp
public string WebSite { get; } = "csdn";
```
\ No newline at end of file
{
"type": "code_options",
"author": "marin1993",
"source": "ExpressionBodyDefinition.md",
"notebook_enable": false,
"exercise_id": "760ffc676fab45b4a8b0485240ca16ad"
}
\ No newline at end of file
# 表达式主体定义
表达式主体函数是 C# 6.0 中的另一个语法精简形式。有一些函数不包括语句体,而是以函数声明后跟表达式的形式来实现。
表达式主体定义具有下列常规语法:
```csharp
member => expression;
```
其中 expression 是有效的表达式。 expression 的返回类型必须可隐式转换为成员的返回类型。 如果成员:
- 具有 void 返回类型或
- 是一个:
- 构造函数
- 终结器
- 属性或索引器 set 访问器
expression 必须是语句表达式。 由于表达式的结果被丢弃,该表达式的返回类型可以是任何类型。
以下示例演示了用于 FingerPrint.ToString 方法的表达式主体定义:
```csharp
public class FingerPrint
{
public DateTime TimeStamp { get; } = DateTime.UtcNow;
public string Process { get; } =
System.Diagnostics.Process.GetCurrentProcess().ProcessName;
public override string ToString() => $"{Process} 时间: {TimeStamp}".Trim();
}
```
它是以下方法定义的简写版:
```csharp
public override string ToString()
{
return $"{Process} 时间: {TimeStamp}".Trim();
}
```
以下关于表达式主体定义描述正确的是:
## 答案
可以为方法、运算符、只读属性、构造函数、终结器以及属性和索引器访问器创建表达式正文定义。
## 选项
### A
仅可以为方法、运算符、只读属性以及属性和索引器访问器创建表达式正文定义。
### B
仅可以为方法、运算符、终结器以及属性和索引器访问器创建表达式正文定义。
### C
仅可以为方法、只读属性、构造函数、终结器以及属性和索引器访问器创建表达式正文定义。
\ No newline at end of file
{
"type": "code_options",
"author": "marin1993",
"source": "Interpolated.md",
"notebook_enable": false,
"exercise_id": "99750c3127d84a3387ab5614494859fd"
}
\ No newline at end of file
# $ - 字符串内插
`$` 特殊字符将字符串文本标识为内插字符串 。 内插字符串是可能包含内插表达式的字符串文本 。 将内插字符串解析为结果字符串时,带有内插表达式的项会替换为表达式结果的字符串表示形式。
字符串内插为格式化字符串提供了一种可读性和便捷性更高的方式。 它比字符串复合格式设置更容易阅读。 比较一下下面的示例,它使用了这两种功能产生相同的输出:
```csharp
string name = "CSDN";
var date = DateTime.Now;
// 复合格式 Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// 内插字符串 String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// 两者输出相同:
// Hello, CSDN! Today is Saturday, it's 14:25 now.
```
以下代码打印九九乘法表正确的是:
## 答案
```csharp
IEnumerable<int> number = Enumerable.Range(1, 9);
foreach (int num in number)
{
for (var i = 1; i <= num; i++)
{
Console.Write($"{i} x {num} = {num * i}\t");
}
Console.WriteLine();
}
```
## 选项
### A
```csharp
IEnumerable<int> number = Enumerable.Range(1, 10);
foreach (int num in number)
{
for (var i = 1; i <= num; i++)
{
Console.Write($"{i} x {num} = {num * i}\t");
}
Console.WriteLine();
}
```
### B
```csharp
IEnumerable<int> number = Enumerable.Range(1, 10);
foreach (int num in number)
{
for (var i = 1; i < num; i++)
{
Console.Write($"{i} x {num} = {num * i}\t");
}
Console.WriteLine();
}
```
### C
```csharp
IEnumerable<int> number = Enumerable.Range(1, 9);
foreach (int num in number)
{
for (var i = 1; i < num; i++)
{
Console.Write($"{i} x {num} = {num * i}\t");
}
Console.WriteLine();
}
```
{
"type": "code_options",
"author": "marin1993",
"source": "KeywordsWhen.md",
"notebook_enable": false,
"exercise_id": "de587a15603f4abcb6409cf3b8b44f65"
}
\ No newline at end of file
# 异常筛选器
关键字 `when` 可用于 语句中 `catch` ,以指定处理程序必须为 true 的条件才能执行特定的异常。 语法为:
```csharp
catch (ExceptionType [e]) when (expr)
```
其中,expr 是一个表达式,其计算结果为布尔值。 如果该表达式返回 true,则执行异常处理程序;如果返回 false,则不执行。
以下示例使用 when 关键字有条件地执行 HttpRequestException 的处理程序,具体取决于异常消息的文本内容。
```csharp
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static void Main()
{
Console.WriteLine(MakeRequest().Result);
}
public static async Task<string> MakeRequest()
{
var client = new HttpClient();
var streamTask = client.GetStringAsync("https://localHost:10000");
try
{
var responseText = await streamTask;
return responseText;
}
catch (HttpRequestException e) when (e.Message.Contains("301"))
{
return "Site Moved";
}
catch (HttpRequestException e) when (______________)
{
return "Page Not Found";
}
catch (HttpRequestException e)
{
return e.Message;
}
}
}
```
请阅读上方代码,补全空格中的内容,以下哪个选项合适?
## 答案
```csharp
e.Message.Contains("404")
```
## 选项
### A
```csharp
e.Message.Contains("500")
```
### B
```csharp
e is null
```
### C
```csharp
e is not null
```
\ No newline at end of file
{
"type": "code_options",
"author": "marin1993",
"source": "Nameof.md",
"notebook_enable": false,
"exercise_id": "19655360d0654abdba2920f2d50b2f86"
}
\ No newline at end of file
# nameof 表达式
`nameof` 表达式可生成变量、类型或成员的名称作为字符串常量:
```csharp
Console.WriteLine(nameof(System.Collections.Generic)); // output: Generic
Console.WriteLine(nameof(List<int>)); // output: List
Console.WriteLine(nameof(List<int>.Count)); // output: Count
Console.WriteLine(nameof(List<int>.Add)); // output: Add
var numbers = new List<int> { 1, 2, 3 };
Console.WriteLine(nameof(numbers)); // output: numbers
Console.WriteLine(nameof(numbers.Count)); // output: Count
Console.WriteLine(nameof(numbers.Add)); // output: Add
```
关于 nameof 表达式以下说法错误的是:
## 答案
不可以在方法或其参数的属性中使用具有方法参数的 nameof 表达式
## 选项
### A
nameof 表达式在编译时进行求值,在运行时无效
### B
nameof 表达式可生成变量、类型或成员的名称作为字符串常量
### C
nameof 可以在某些情况下避免人为的拼写错误
\ No newline at end of file
{
"type": "code_options",
"author": "marin1993",
"source": "NullConditionalOperators.md",
"notebook_enable": false,
"exercise_id": "7da9652f3f5e4f5ca27099fed4d61710"
}
\ No newline at end of file
# Null 条件运算符 ?. 和 ?[]
即使是 .NET 开发新手,也可能非常熟悉 `NullReferenceException`。无一例外的是开发中几乎都会出现这个 Bug,因为开发人员在调用 (null) 对象的成员之前未进行充分的 null 检查。
Null 条件运算符 `?.``?[]`,可帮助您更加简便地编写这些检查。
仅当操作数的计算结果为非 NULL 时,NULL 条件运算符才对其操作数应用成员访问?. 或元素访问 `?[]` 操作;否则,它会返回 null。 即:
- 如果 a 的计算结果为 null,则 `a?.x``a?[x]` 的结果为 null。
- 如果 a 的计算结果为非 null,则 `a?.x``a?[x]` 的结果将分别与 `a.x``a[x]` 的结果相同。
> 如果 `a.x` 或 `a[x]` 引发异常,则 `a?.x` 或 `a?[x]` 将对非 null a 引发相同的异常。 例如,如果 a 为非 null 数组实例且 x 在 a的边界之外,则 `a?[x]` 将引发 `IndexOutOfRangeException。`
以下示例演示了 `?.``?[]` 运算符的用法:
```csharp
double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum)
{
return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN;
}
var sum1 = SumNumbers(null, 0);
Console.WriteLine(sum1); // output: NaN
var numberSets = new List<double[]>
{
new[] { 1.0, 2.0, 3.0 },
null
};
var sum2 = SumNumbers(numberSets, 0);
Console.WriteLine(sum2); // output: 6
var sum3 = SumNumbers(numberSets, 1);
Console.WriteLine(sum3); // output: NaN
```
## 答案
使用 `a?.x``a?[x]` 进行条件运算不会发生异常
## 选项
### A
Null 条件成员访问运算符 `?.` 也称为 Elvis 运算符
### B
可以使用 `?.` 运算符来检查委托是否非 null 并以线程安全的方式调用它
### C
`?.` 运算符对其左操作数的计算不超过一次
......@@ -14,7 +14,14 @@
"表达式方法体"
],
"children": [],
"export": [],
"export": [
"KeywordsWhen.json",
"AutoPropertyInitializers.json",
"Nameof.json",
"NullConditionalOperators.json",
"Interpolated.json",
"ExpressionBodyDefinition.json"
],
"keywords_must": [],
"keywords_forbid": []
}
\ No newline at end of file

using System.Xml.Linq;
internal class Program
{
private static void Main(string[] args)
{
string name = "CSDN";
var date = DateTime.Now;
// 复合格式 Composite formatting:
Console.WriteLine("Hello, {0}! Today is {1}, it's {2:HH:mm} now.", name, date.DayOfWeek, date);
// 内插字符串 String interpolation:
Console.WriteLine($"Hello, {name}! Today is {date.DayOfWeek}, it's {date:HH:mm} now.");
// 两者输出相同:
// Hello, CSDN! Today is Wednesday, it's 19:40 now.
// 九九乘法表
IEnumerable<int> number = Enumerable.Range(1, 9);
foreach (int num in number)
{
for (var i = 1; i <= num; i++)
{
Console.Write($"{i} x {num} = {num * i}\t");
}
Console.WriteLine();
}
// nameof 表达式
Console.WriteLine(nameof(System.Collections.Generic)); // output: Generic
Console.WriteLine(nameof(List<int>)); // output: List
Console.WriteLine(nameof(List<int>.Count)); // output: Count
Console.WriteLine(nameof(List<int>.Add)); // output: Add
var numbers = new List<int> { 1, 2, 3 };
Console.WriteLine(nameof(numbers)); // output: numbers
Console.WriteLine(nameof(numbers.Count)); // output: Count
Console.WriteLine(nameof(numbers.Add)); // output: Add
Console.WriteLine(nameof(FingerPrint)); // output: FingerPrint
// 自动属性初始化表达式
var fi = new FingerPrint();
Console.WriteLine($"{fi.Process} \t {fi.TimeStamp}");
// Null 条件运算符 ?. 和 ?[]
double SumNumbers(List<double[]> setsOfNumbers, int indexOfSetToSum)
{
return setsOfNumbers?[indexOfSetToSum]?.Sum() ?? double.NaN;
}
var sum1 = SumNumbers(null, 0);
Console.WriteLine(sum1); // output: NaN
var numberSets = new List<double[]>{
new[] { 1.0, 2.0, 3.0 },
null
};
var sum2 = SumNumbers(numberSets, 0);
Console.WriteLine(sum2); // output: 6
var sum3 = SumNumbers(numberSets, 1);
Console.WriteLine(sum3); // output: NaN
//表达式主体定义
Console.WriteLine(fi.ToString());
}
}
public class FingerPrint
{
public DateTime TimeStamp { get; } = DateTime.UtcNow;
public string Process { get; } =
System.Diagnostics.Process.GetCurrentProcess().ProcessName;
public override string ToString() => $"{Process} 时间: {TimeStamp}".Trim();
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
......@@ -23,7 +23,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -36,7 +38,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -48,7 +52,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -61,7 +67,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -72,12 +80,16 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -93,7 +105,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -107,7 +121,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -118,7 +134,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -130,7 +148,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -141,7 +161,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -152,7 +174,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -165,7 +189,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -177,12 +203,16 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -203,7 +233,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -217,22 +249,24 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
"C#5.0特性": {
"node_id": "csharp-730d385761c64702b710308c91271c9c",
"keywords": [
"case支持表达式",
"带参数的泛型构造函数",
"扩展属性",
"支持null类型运算",
"绑定运算符,:=:"
"async",
"await",
"caller information"
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -253,7 +287,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -280,7 +316,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -296,7 +334,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -316,7 +356,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -340,17 +382,23 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -371,7 +419,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -382,7 +432,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -393,7 +445,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -404,7 +458,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -415,12 +471,16 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -436,7 +496,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -447,7 +509,9 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -458,12 +522,16 @@
],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -472,7 +540,9 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -481,7 +551,9 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -490,7 +562,9 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -499,12 +573,16 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -518,7 +596,9 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
},
{
......@@ -527,16 +607,22 @@
"keywords": [],
"children": [],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
],
"keywords_must": [],
"keywords_forbid": []
"keywords_forbid": [],
"group": 0,
"subtree": ""
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册