提交 c7d50f6b 编写于 作者: Admini$trat0r's avatar Admini$trat0r

添加 1..NET初阶\3.C#特性\1.C#3.0特性 部分特性

修改 部分错别字
上级 77461c54
......@@ -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
......@@ -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
{
"type": "code_options",
"author": "Gao996",
"source": "ExpressionTree.md",
"exercise_id": "ea23a2f4010a4dd398b477d4990547a7",
"notebook_enable": false
}
\ No newline at end of file
# C# 3.0 特性 表达式树
表达式树是定义代码的数据结构。 它们基于编译器用于分析代码和生成已编译输出的相同结构。
以下代码创建了一个表达式并且执行了,Compile方法可以编译表达式树由描述为可执行代码的 lambda 表达式,并生成一个委托,表示 lambda 表达式。
注意这里不能使用var来声明此表达式树,因为此操作无法执行,这是由于赋值右侧是隐式类型而导致的。
```csharp
Expression<Func<int>> 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
{
"type": "code_options",
"author": "Gao996",
"source": "ExtensionMethod.md",
"exercise_id": "2b4114f71e59455099471a65d2b0885f",
"notebook_enable": false
}
\ No newline at end of file
# 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
{
"type": "code_options",
"author": "Gao996",
"source": "LambdaExpression.md",
"exercise_id": "99b3ef51bbb74e46b987729b18c9e311",
"notebook_enable": false
}
\ No newline at end of file
# C# 3.0 特性 Lambda 表达式
Lambda表达式可采用表达式为其主体,也可以采用语句块作为其主体:
```csharp
(input-parameters) => expression
(input-parameters) => { <sequence-of-statements> }
```
Lambda表达式可以没有参数,如果有参数需要在左侧添加,任何 Lambda 表达式都可以转换为委托类型。
在下列选项中,没有编译错误并且可以输出5的平方的是:
## 答案
```csharp
Func<int, int> square = x => x * x;
Console.WriteLine(square(5));
```
## 选项
### A
```csharp
Func<int, int> square = () => x * x;
Console.WriteLine(square(5));
```
### B
```csharp
Func<int, int> square = x * x;
Console.WriteLine(square(5));
```
### C
```csharp
Func<int, int> square => x * x;
Console.WriteLine(square(5));
```
\ No newline at end of file
......@@ -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
......
......@@ -36,8 +36,32 @@ orderby example.Count descending
select $"{example.Name}\t{example.Count}";
// Lambda 表达式
Func<int, int> square = x => x * x;
Console.WriteLine(square(5));
// 表达式树
Expression<Func<int>> 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
......@@ -102,7 +102,7 @@ public class TestController : ControllerBase
}
```
如上代码,在下列选中,可以正确获取Test接口的是:
如上代码,在下列选中,可以正确获取Test接口的是:
## 答案
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册