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

新增C# 3.0特性 的部分(自动实现的属性,匿名类型,查询表达式)

上级 e68968c4
{
"type": "code_options",
"author": "Gao996",
"source": "AnonymousType.md",
"exercise_id": "47413f9f257545289bc30c177ce14d37",
"notebook_enable": false
}
\ No newline at end of file
# 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
{
"type": "code_options",
"author": "Gao996",
"source": "AutoImplementedProperties.md",
"exercise_id": "ce894b0d3ce14592a0af56e06a32854d",
"notebook_enable": false
}
\ No newline at end of file
# 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
{
"type": "code_options",
"author": "Gao996",
"source": "QueryExpression.md",
"exercise_id": "20602ef8b2bf49cab1a5f3ff66523b10",
"notebook_enable": false
}
\ No newline at end of file
# 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
// 自动实现的属性
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
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册