Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
skill_tree_dotnet
提交
e1dcb664
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e1dcb664
编写于
3月 14, 2022
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加C#4。0
上级
f2247b80
变更
12
显示空白变更内容
内联
并排
Showing
12 changed file
with
353 addition
and
5 deletion
+353
-5
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.json
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.json
+8
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.md
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.md
+69
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.json
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.json
+8
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.md
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.md
+79
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.json
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.json
+8
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.md
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.md
+42
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.json
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.json
+8
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.md
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.md
+42
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/config.json
data/1..NET初阶/3.C#特性/2.C#4.0特性/config.json
+9
-3
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/Program.cs
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/Program.cs
+68
-0
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/sample.csproj
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/sample.csproj
+10
-0
data/tree.json
data/tree.json
+2
-2
未找到文件。
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.json
0 → 100644
浏览文件 @
e1dcb664
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"Contravariance.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"12348524087540ceb975cd9f2d00108f"
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/Contravariance.md
0 → 100644
浏览文件 @
e1dcb664
# 逆变(Contravariance)
假设有一个父类和一个子类:
```
csharp
// 父类和子类
class
Article
{
public
string
ToString
(){
return
"article"
;
}
}
class
CodeArticle
:
Article
{
public
string
ToString
(){
return
"article with code"
;
}
}
```
那么,通常针对父类的方法是可以传入子类实例的,4.0增加了
`逆变(Contravariance)`
,也就是父类的泛型实现可以转成子类的泛型接口。
现在有如下代码:
```
csharp
// 逆变
public
interface
ISend
<
in
T
>
{
void
Send
(
T
t
);
}
public
class
ArticleSender
<
T
>
:
ISend
<
T
>{
public
void
Send
(
T
t
){
Console
.
WriteLine
(
"Send:{0}"
,
t
.
ToString
());
}
}
```
以下代码正确的是?
## 答案
```
csharp
ISend
<
CodeArticle
>
sender
=
new
ArticleSender
<
Article
>();
sender
.
Send
(
new
CodeArticle
());
```
## 选项
### A
```
csharp
ISend
<
Article
>
sender
=
new
ArticleSender
<
CodeArticle
>();
sender
.
Send
(
new
Article
());
```
### B
```
csharp
ISend
<
Article
>
sender
=
new
ArticleSender
<
CodeArticle
>();
sender
.
Send
(
new
CodeArticle
());
```
### C
```
csharp
ISend
<
CodeArticle
>
sender
=
new
ArticleSender
<
Article
>();
sender
.
Send
(
new
Article
());
```
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.json
0 → 100644
浏览文件 @
e1dcb664
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"Covariance.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"0b9afd8bc5ac4139af83b90f7211d41a"
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/Covariance.md
0 → 100644
浏览文件 @
e1dcb664
# 协变(Covariance)
假设有一个父类和一个子类:
```
csharp
// 父类和子类
class
Article
{
public
string
ToString
(){
return
"article"
;
}
}
class
CodeArticle
:
Article
{
public
string
ToString
(){
return
"article with code"
;
}
}
```
那么,通常可以把子类当作父类来使用,但是如果包了一层泛型在4.0之前是不可以的:
```
csharp
IEnumerable
<
object
>
objs
=
new
List
<
string
>
{
"Keep it simple, stupid"
};
```
4.
0增加了
`协变(Covariance)`
特性后则可以支持上述代码,也就是子类的泛型可以转成父类的接口。
现在有如下代码:
```
csharp
// 协变
public
interface
ISearch
<
out
T
>
{
T
Search
();
}
public
class
CodeArticleSearch
<
T
>:
ISearch
<
T
>
where
T
:
new
(){
public
T
Search
(){
return
new
T
();
}
}
```
以下代码正确的是?
## 答案
```
csharp
ISearch
<
Article
>
search
=
new
CodeArticleSearch
<
CodeArticle
>();
Article
article
=
search
.
Search
();
Console
.
WriteLine
(
article
);
```
## 选项
### A
```
csharp
ISearch
<
CodeArticle
>
search
=
new
CodeArticleSearch
<
Article
>();
Article
article
=
search
.
Search
();
Console
.
WriteLine
(
article
);
```
### B
```
csharp
ISearch
<
Article
>
search
=
new
CodeArticleSearch
<
CodeArticle
>();
CodeArticle
article
=
search
.
Search
();
Console
.
WriteLine
(
article
);
```
### C
```
csharp
ISearch
<
CodeArticle
>
search
=
new
CodeArticleSearch
<>(
Article
);
CodeArticle
article
=
search
.
Search
();
Console
.
WriteLine
(
article
);
```
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.json
0 → 100644
浏览文件 @
e1dcb664
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"NamedParam.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"eb2c4ef49ddc42feb74258f81f3c3504"
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/NamedParam.md
0 → 100644
浏览文件 @
e1dcb664
# 命名参数
4.
0引入了命名参数,命名参数让我们可以在调用方法时指定参数名字来给参数赋,还可以调整参数顺序
```
csharp
static
class
Test
{
public
static
List
<
string
>
Find
(
string
key
,
int
number
,
bool
sort
){
Console
.
WriteLine
(
"key:{0}, number:{1}, sort:{2}"
,
key
,
number
,
sort
);
return
new
List
<
string
>(){};
}
}
```
以下对函数调用错误的是?
## 答案
```
csharp
Test
.
Find
(
key
:
"hello"
,
number
:
5
);
```
## 选项
### A
```
csharp
Test
.
Find
(
key
:
"hello"
,
number
:
5
,
sort
:
false
);
```
### B
```
csharp
Test
.
Find
(
key
:
"hello"
,
sort
:
false
,
number
:
5
);
```
### C
```
csharp
Test
.
Find
(
sort
:
false
,
number
:
5
,
key
:
"hello"
);
```
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.json
0 → 100644
浏览文件 @
e1dcb664
{
"type"
:
"code_options"
,
"author"
:
"huanhuilong"
,
"source"
:
"OptionParam.md"
,
"notebook_enable"
:
false
,
"exercise_id"
:
"0cccd130210a4af6aea30df02619a70d"
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/OptionParam.md
0 → 100644
浏览文件 @
e1dcb664
# 可选参数
4.
0引入了可选参数,如下的函数,示例代码如下
```
csharp
static
class
Test
{
public
static
List
<
string
>
Find
(
string
key
,
int
number
=
5
,
bool
sort
=
false
){
Console
.
WriteLine
(
"key:{0}, number:{1}, sort:{2}"
,
key
,
number
,
sort
);
return
new
List
<
string
>(){};
}
}
```
以下对函数调用错误的是?
## 答案
```
csharp
Test
.
Find
(
"hello"
,
true
,
10
);
```
## 选项
### A
```
csharp
Test
.
Find
(
"hello"
);
```
### B
```
csharp
Test
.
Find
(
"hello"
,
10
);
```
### C
```
csharp
Test
.
Find
(
"hello"
,
10
,
true
);
```
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/config.json
浏览文件 @
e1dcb664
{
"node_id"
:
"csharp-d4650354f5f5498e9969fd98bbb40323"
,
"keywords"
:
[
"互操作特性"
,
"协变和逆变"
,
"命名参数和可选参数"
,
"命名参数"
,
"可选参数"
,
"动态查找"
],
"children"
:
[],
"export"
:
[],
"export"
:
[
"OptionParam.json"
,
"NamedParam.json"
,
"Covariance.json"
,
"Contravariance.json"
],
"keywords_must"
:
[],
"keywords_forbid"
:
[]
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/Program.cs
0 → 100644
浏览文件 @
e1dcb664
// See https://aka.ms/new-console-template for more information
Console
.
WriteLine
(
"Hello, World!"
);
// // // 协变代码
// ISearch<Article> search = new CodeArticleSearch<CodeArticle>();
// Article article = search.Search();
// Console.WriteLine(article);
// // 逆变代码
// ISend<CodeArticle> sender = new ArticleSender<Article>();
// sender.Send(new CodeArticle());
// Test.Find("hello");
// Test.Find("hello", 10);
// Test.Find("hello", 10, true);
// Test.Find("hello", true, 10);
// Test.Find(key:"hello", number:5);
Test
.
Find
(
key
:
"hello"
,
sort
:
false
,
number
:
5
);
Test
.
Find
(
sort
:
false
,
number
:
5
,
key
:
"hello"
);
static
class
Test
{
public
static
List
<
string
>
Find
(
string
key
,
int
number
,
bool
sort
){
Console
.
WriteLine
(
"key:{0}, number:{1}, sort:{2}"
,
key
,
number
,
sort
);
return
new
List
<
string
>(){};
}
}
// 父类和子类
class
Article
{
public
string
ToString
(){
return
"article"
;
}
}
class
CodeArticle
:
Article
{
public
string
ToString
(){
return
"article with code"
;
}
}
// 协变
public
interface
ISearch
<
out
T
>
{
T
Search
();
}
public
class
CodeArticleSearch
<
T
>:
ISearch
<
T
>
where
T
:
new
(){
public
T
Search
(){
return
new
T
();
}
}
// 逆变
public
interface
ISend
<
in
T
>
{
void
Send
(
T
t
);
}
public
class
ArticleSender
<
T
>
:
ISend
<
T
>{
public
void
Send
(
T
t
){
Console
.
WriteLine
(
"Send:{0}"
,
t
.
ToString
());
}
}
\ No newline at end of file
data/1..NET初阶/3.C#特性/2.C#4.0特性/sample/sample.csproj
0 → 100644
浏览文件 @
e1dcb664
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
data/tree.json
浏览文件 @
e1dcb664
...
...
@@ -210,9 +210,9 @@
"C#4.0特性"
:
{
"node_id"
:
"csharp-d4650354f5f5498e9969fd98bbb40323"
,
"keywords"
:
[
"互操作特性"
,
"协变和逆变"
,
"命名参数和可选参数"
,
"命名参数"
,
"可选参数"
,
"动态查找"
],
"children"
:
[],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录