Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_dotnet
提交
520285ba
S
skill_tree_dotnet
项目概览
CSDN 技术社区
/
skill_tree_dotnet
通知
30
Star
6
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
skill_tree_dotnet
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
520285ba
编写于
12月 22, 2021
作者:
Admini$trat0r
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
新增C# 3.0特性 的部分(自动实现的属性,匿名类型,查询表达式)
上级
e68968c4
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
235 addition
and
0 deletion
+235
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.json
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.json
+8
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.md
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.md
+49
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.json
.../1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.json
+8
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.md
data/1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.md
+56
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.json
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.json
+8
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.md
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.md
+52
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/Program.cs
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/Program.cs
+44
-0
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/sample.csproj
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/sample.csproj
+10
-0
未找到文件。
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.json
0 → 100644
浏览文件 @
520285ba
{
"type"
:
"code_options"
,
"author"
:
"Gao996"
,
"source"
:
"AnonymousType.md"
,
"exercise_id"
:
"47413f9f257545289bc30c177ce14d37"
,
"notebook_enable"
:
false
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/1.C#3.0特性/AnonymousType.md
0 → 100644
浏览文件 @
520285ba
# 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
data/1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.json
0 → 100644
浏览文件 @
520285ba
{
"type"
:
"code_options"
,
"author"
:
"Gao996"
,
"source"
:
"AutoImplementedProperties.md"
,
"exercise_id"
:
"ce894b0d3ce14592a0af56e06a32854d"
,
"notebook_enable"
:
false
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/1.C#3.0特性/AutoImplementedProperties.md
0 → 100644
浏览文件 @
520285ba
# 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
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.json
0 → 100644
浏览文件 @
520285ba
{
"type"
:
"code_options"
,
"author"
:
"Gao996"
,
"source"
:
"QueryExpression.md"
,
"exercise_id"
:
"20602ef8b2bf49cab1a5f3ff66523b10"
,
"notebook_enable"
:
false
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/1.C#3.0特性/QueryExpression.md
0 → 100644
浏览文件 @
520285ba
# 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
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/Program.cs
0 → 100644
浏览文件 @
520285ba
// 自动实现的属性
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
data/1..NET初阶/3.C#特性/1.C#3.0特性/sample/sample.csproj
0 → 100644
浏览文件 @
520285ba
<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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录