简体   繁体   English

“=>”是什么意思(在函数/属性上下文中)?

[英]What does '=>' mean (in functions / property context)?

I got an auto generated code using Lambdas while developing a Xamarin application:在开发 Xamarin 应用程序时,我使用 Lambdas 获得了自动生成的代码:

public override string this[int position] => throw new NotImplementedException();

public override int Count => throw new NotImplementedException();

What does the => operator mean in this context? =>运算符在这种情况下是什么意思?

Thanks R感谢 R

These are not lambdas, they are Expression-bodied Members !这些不是 lambda,它们是Expression-bodied Members

In the context of a property, these are basically the getters of a property simplified to become a single expression (as opposed to a whole statement).在属性的上下文中,这些基本上是简化为单个表达式(而不是整个语句)的属性的 getter。

This:这个:

public override int Count => throw new NotImplementedException();

Is equivalent to:相当于:

public override int Count {
    get { throw new NotImplementedException(); }
}

As @sweeper says in your example they do not relate to lambda expressions as they are expression body operators (which were introduced in C# 6 and expanded on in 7).正如@sweeper 在您的示例中所说,它们与 lambda 表达式无关,因为它们是表达式主体运算符(在 C# 6 中引入并在 7 中扩展)。 It is also used to indicate a lambda expression though, so it's usage is two fold.不过,它也用于表示 lambda 表达式,因此它的用法有两种。

Further information on each usage of the => operator can be found here;有关=>运算符的每种用法的更多信息,请参见此处; https://learn.microsoft.com/en-us/do.net/csharp/language-reference/operators/lambda-operator https://learn.microsoft.com/en-us/do.net/csharp/language-reference/operators/lambda-operator

First, let's clarify that the => operator is currently used in two different contexts:首先,让我们澄清一下=>运算符目前在两种不同的上下文中使用:

  1. Lambda expressions. Lambda 个表达式。 Often you will see them in Linq, eg通常您会在 Linq 中看到它们,例如
    var query = Customers.OrderBy(x => x.CompanyName);

  2. Expression bodied functions.表达式体函数。 This is what we have here.这就是我们这里所拥有的。

In order to understand what => means, please take a look at the following simple example:为了理解=>的含义,请看下面的简单示例:

using System;

public class Program
{
    public void Main()
    {
        var obj = new Test();
        obj.Count.Dump();
        obj[7].Dump();
    }


    class Test
    {
        public int Count => 1;
        public string this[int position] => $"2 x {position} = {(2*position)}";
    }
}

Dumping object(Int32)倾倒对象(Int32)
1 1个
Dumping object(String)转储对象(字符串)
2 x 7 = 14 2×7 = 14

Try it in DotNetFiddle在 DotNetFiddle 中尝试

Here, the NotImplementedException code, which is just there to tell you (the developer) that the property and indexer is not implemented but should be, is replaced by some function:在这里, NotImplementedException代码,它只是告诉你(开发人员)属性和索引器没有实现但应该被实现,被一些 function 取代:

  • Count is a readonly property returning always 1 Count 是一个只读属性,总是返回 1
  • whenever you apply [... ] to the object, the doubled index is returned每当您将[... ]应用于 object 时,都会返回双倍索引

Note that in earlier versions of C# you had to write:请注意,在 C# 的早期版本中,您必须编写:

class Test
{
        public int Count { get { return 1; } }
        public string this[int position] { 
            get { return String.Format("2 x {0} = {1}", 
                                       position, (2*position).ToString()); }}
}

which is equivalent to the code above.这相当于上面的代码。 So in essence in C#7 you have to type much less to achieve the same result.所以本质上,在 C#7 中,您必须输入更少的代码才能获得相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM