ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# 11 中的新增功能:概述

2022-10-31 17:53:26  阅读:319  来源: 互联网

标签:C++ java JavaScript python 管理


C# 11 即将推出,因此我们将详细探讨其新功能。您可能会发现这些新功能非常好奇,即使它们并不多。今天,让我们仔细看看泛型数学支持、原始字符串文字、所需的修饰符、属性中的类型参数等。

通用属性

C# 11 添加了对泛型属性的支持 — 现在我们可以像泛型类和方法一样声明它们。尽管我们之前已经能够在构造函数中将类型作为参数传递,但现在我们可以使用约束来指定应传递哪些类型。现在我们也不必一直使用运算符。wheretypeof

这就是它在“装饰器”模式的简单实现示例中的工作方式。让我们定义泛型属性:

C#
1
[AttributeUsage(AttributeTargets.Class)]

2
public class DecorateAttribute<T> : Attribute where T : class

3
{

4
    public Type DecoratorType{ get; set; }

5
    public DecorateAttribute()

6
    {

7
        DecoratorType = typeof(T);

8
    }

9
}

 

接下来,我们实现一个层次结构(根据模式)和一个工厂来创建装饰对象。注意装饰属性:

C#
1
public interface IWorker

2
{

3
    public void Action();

4
}

5
public class LoggerDecorator : IWorker

6
{

7
    private IWorker _worker;

8
    public LoggerDecorator(IWorker worker)

9
    {

10
        _worker = worker;

11
    }

12
    public void Action()

13
    {

14
        Console.WriteLine("Log before");

15
        _worker.Action();

16
        Console.WriteLine("Log after");

17
    }

18
}

19
[Decorate<LoggerDecorator>]

20
public class SimpleWorker : IWorker

21
{

22
    public void Action()

23
    {

24
        Console.WriteLine("Working..");

25
    }

26
}

27

28
public static class WorkerFactory

29
{

30
    public static IWorker CreateWorker()

31
    {

32
        IWorker worker = new SimpleWorker();

33

34
        if (typeof(SimpleWorker)

35
            .GetCustomAttribute<DecorateAttribute<LoggerDecorator>>() != null)

36
        {

37
            worker = new LoggerDecorator(worker);

38
        }

39

40
        return worker;

41
    }

42
}

 

让我们看看它是如何工作的:

C#
1
var worker = WorkerFactory.CreateWorker();

2

3
worker.Action();

4
// Log before

5
// Working..

6
// Log after

 

请注意,限制尚未完全删除,应指定类型。例如,不能使用类的类型参数:

C#
1
public class GenericAttribute<T> : Attribute { }

2
public class GenericClass<T>

3
{

4
    [GenericAttribute<T>]

5
    //Error CS8968 'T': an attribute type argument cannot use type parameters

6
    public void Action()

7
    {

 

You may ask, are GenericAttribute<int> and GenericAttribute<string> different attributes, or just multiple uses of the same one? Microsoft determined them to be the same attribute. This means, to use the attribute multiple times, you need to set the AllowMultiple property to true. Let's modify the above example:

C#
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public class DecorateAttribute<T> : Attribute where T : clas

Now the bject can be decorated several times:

C#
[Decorate<LoggerDecorator>]
[Decorate<TimerDecorator>]
public class SimpleWorker : IWorker

显然,这个新功能不是基本的,但库开发人员现在可以在属性中使用泛型时创建更加用户友好的界面。

通用数学支持

在新版本的 C# 语言中,我们可以对泛型类型使用数学运算。

此新功能会导致两个常规后果:

  • 接口的静态成员现在可以具有抽象修饰符,该修饰符需要导数来实现相应的静态方法;
  • 这就是为什么我们现在可以在接口中声明算术运算符。

现在我们得到了一个静态的抽象结构,可能看起来有点奇怪,但我不会说它完全没有意义。让我们检查一下此更新的实际结果。看看这个稍微做作的自然数实现示例(数字可以从字符串中相加和解析):

C#public interface IAddable<TLeft, TRight, TResult>
    where TLeft : IAddable<TLeft, TRight, TResult>
    static abstract TResult operator +(TLeft left, TRight right);
public interface IParsable<T> where T : IParsable<T>
    static abstract T Parse(string s);
public record Natural : IAddable<Natural, Natural, Natural>, IParsable<Natural>
    public int Value { get; init; } = 0;
    public static Natural Parse(string s)
    return new() { Value = int.Parse(s) };
    public static Natural operator +(Natural left, Natural right)
        return new() { Value = left.Value + right.Value };

标签:C++,java,JavaScript,python,管理
来源:

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有