提交 e1dcb664 编写于 作者: F feilong

增加C#4。0

上级 f2247b80
{
"type": "code_options",
"author": "huanhuilong",
"source": "Contravariance.md",
"notebook_enable": false,
"exercise_id": "12348524087540ceb975cd9f2d00108f"
}
\ No newline at end of file
# 逆变(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
{
"type": "code_options",
"author": "huanhuilong",
"source": "Covariance.md",
"notebook_enable": false,
"exercise_id": "0b9afd8bc5ac4139af83b90f7211d41a"
}
\ No newline at end of file
# 协变(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
{
"type": "code_options",
"author": "huanhuilong",
"source": "NamedParam.md",
"notebook_enable": false,
"exercise_id": "eb2c4ef49ddc42feb74258f81f3c3504"
}
\ No newline at end of file
# 命名参数
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
{
"type": "code_options",
"author": "huanhuilong",
"source": "OptionParam.md",
"notebook_enable": false,
"exercise_id": "0cccd130210a4af6aea30df02619a70d"
}
\ No newline at end of file
# 可选参数
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
{
"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
// 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
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
......@@ -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.
先完成此消息的编辑!
想要评论请 注册