C# Lambda 令人费解的行为

发布于 2024-08-09 03:50:36 字数 829 浏览 2 评论 0原文

Func<Classification, string> test1 = c => c.Id = "x"; 
Func<Classification, string> test2 = c => { return c.Id = "x";}; 

我已经使用 lambda 工作了近一年左右,并且对它们相当合理,但今天我正在查看 NBuilder 并看到一个奇怪的 Func ,它似乎与示例不匹配。无论如何我都玩过并且它检查了但我不明白为什么上面的编译更不用说运行了。我们正在做一个作业,因此表达式不会计算出任何值,对吗???所以

我想也许我错过了与 lambda 相关的东西,所以我尝试了其他方法:

    [Test]
    public void AmIGoingMad()
    {
        Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?
    }

    public string Test()
    {
        string subject = "";
        return subject = "Matt";
    }

果然 AmIGoingMad 失败了,并且实际上返回了“Matt”。

为什么我们会有这种行为?这是在哪里记录的?它纯粹是一个语法快捷方式吗?

我觉得我错过了对 lambda 甚至 C# 的一些基本理解。

感觉很蠢。

Func<Classification, string> test1 = c => c.Id = "x"; 
Func<Classification, string> test2 = c => { return c.Id = "x";}; 

I've worked with lambda's for nearly a year or so now and fairly reasonable with them, but today I was looking at NBuilder and seen a weird Func that didn't seem to match the examples. I had play anyway and it checks out but I don't understand why the above compiles let alone runs. We are doing an assignment and thus the expression doesn't evaluate to anything, right??? or not

So I thought maybe something I've missed related to lambda, so I tried something else:

    [Test]
    public void AmIGoingMad()
    {
        Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?
    }

    public string Test()
    {
        string subject = "";
        return subject = "Matt";
    }

Sure enough AmIGoingMad fails and "Matt" is actually returned.

Why do we have this behavior? Where is this documented? Is it purely a syntactic shortcut?

I feel like I missed something fundamental in my understanding of lambda or even C#.

Feeling dumb.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

多孤肩上扛 2024-08-16 03:50:37

赋值语句有一个返回值——该值就是被赋值的值。即使 C 也有这个,所以您可以将赋值链接在一起,如下所示:

a = b = c = d = 10;

对 d 的赋值的返回值是 10,该值被赋值给 c,依此类推。

An assignment statement has a return value--that value is that which was assigned. Even C had this, so you could chain together assignments like the following:

a = b = c = d = 10;

The assignment to d has the return value of 10 which gets assigned to c, and so on.

勿忘初心 2024-08-16 03:50:37

您看到的是赋值链,这可以追溯到 C/C++。它是为了支持这种情况:

int a = b = c = 0;

或者在我实际使用它的地方:

public static IEnumerable<string> ReadLines(string filePath)
{
    using (var rdr = new StreamReader(filePath))
    {
       string line;
       while ( (line = rdr.ReadLine()) != null)  // <-----
       {
          yield return line;
       }
    }
}

What you're seeing is assignment chaining, something that goes way back to C/C++. It's there to support this scenario:

int a = b = c = 0;

Or somewhere I actually use it:

public static IEnumerable<string> ReadLines(string filePath)
{
    using (var rdr = new StreamReader(filePath))
    {
       string line;
       while ( (line = rdr.ReadLine()) != null)  // <-----
       {
          yield return line;
       }
    }
}
吃兔兔 2024-08-16 03:50:37

它之所以有效,是因为赋值 c.Id = "x" 的计算结果为“x”。例如,如果您想在一个语句中分配和检查一个值(有些人认为这是不好的做法),则可以使用它,如下所示:

string s;
if((s = SomeFunction()) != null) { \\do something with s }

It works because the assignment c.Id = "x" evaluates to the value of "x". You can use this for example if you want to assign and check a value in one statement (which some people consider bad practice) like this:

string s;
if((s = SomeFunction()) != null) { \\do something with s }
你是年少的欢喜 2024-08-16 03:50:37

正如其他人所说,赋值本身返回一个值。

您还可以像这样利用它:

private List<string> theList;
public List<string> LazyList
{
  get { return theList ?? (theList = new List<string>()); }
}

As others said, the assignment returns a value itself.

You could also take advantage of it like this:

private List<string> theList;
public List<string> LazyList
{
  get { return theList ?? (theList = new List<string>()); }
}
傾城如夢未必闌珊 2024-08-16 03:50:37

我不明白你的问题。

您发布的代码看起来像是误解了 === 之间的区别,然后明确评论说您希望它使用 =这是赋值运算符。

让我问你一个问题:

为什么你想要或期望它不能编译?

Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?

基本上,你正在这样做:

String temp = Test();
Assert.That(temp, Is.Null);

I don't understand your question.

You post code that looks like you've misunderstood the difference between = and ==, and then explicitly comment that you want it to use = which is the assignment operator.

Let me ask you a question instead:

Why would you want or expect this to not compile?

Assert.That(Test(),Is.Null); // not sure what to expect - compile fail?

Basically, you're doing this:

String temp = Test();
Assert.That(temp, Is.Null);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文