what is this feature called in c# and which version of c# uses this

前端 未结 3 1447
感动是毒
感动是毒 2021-01-29 15:38

I am using this line of code in my project and haven`t seen this syntax.

 internal static StringBuilder a(this StringBuilder sb, string b) => sb.Append(b).Ap         


        
相关标签:
3条回答
  • 2021-01-29 15:58

    This line consists of two main features. The first is an extension method which is the method signature defined to the left of the lambda symbol (=>) and can be identified as an extension method as it uses the this keyword before the first parameter. The second is called an expression bodied function which is the part to the right of the lambda symbol.

    Extension methods

    Expression bodied functions

    0 讨论(0)
  • 2021-01-29 15:59

    internal static StringBuilder a(this StringBuilder sb, string b)

    This part tells us that a is an extension method to StringBuilder. You can read more on extension methods here

    => sb.Append(b).Append("\n")

    These are functions with no statement body. Instead, you implement them with an expression following the function declaration. This is new feature in C# 6. and called expression-bodied function members. You can read more about them here

    0 讨论(0)
  • 2021-01-29 16:07

    If you mean the lambda arrow =>, it is a feature of C# 6 (expression-bodied function members). Check https://github.com/dotnet/roslyn/wiki/New-Language-Features-in-C%23-6#expression-bodied-function-members

    0 讨论(0)
提交回复
热议问题