gpt4 book ai didi

c# - void 表达式没有值吗?

转载 作者:太空宇宙 更新时间:2023-11-03 21:02:23 24 4
gpt4 key购买 nike

简而言之,来自 C#

Expressions and Operators

An expression essentially denotes a value.

...

Void Expressions

A void expression is an expression that has no value. For example:

Console.WriteLine (1)

A void expression, since it has no value, cannot be used as an operand to build more complex expressions:

1 + Console.WriteLine (1)      // Compile-time error
  1. 由于 void 表达式是一个表达式,它必须表示一个值。但怎么会“没有值(value)”呢?
  2. 由于 void 表达式没有值,所以 void 表达式是声明?

谢谢。


阅读 Eric Lippert 对 Can a C# lambda expression ever return void? 的回答后,正如 Rufus L 在评论中指出的那样,我有以下问题:

  • 对方法的调用是否总是一个表达式,其值是方法返回的值?

  • 是否 void 表达式总是调用返回 void 的方法?

最佳答案

Since a void expression is an expression, it must denote a value. But how can it "has no value"?

这个说法是错误的。表达式不需要“表示值”。

大多数表达式都可以。有些没有。特别是:

  • 分类为“无”的表达式,因为它们是返回 void 的方法的调用

  • System.Console.WriteLine("") 中,我们有表达式 System 表示命名空间,而不是值,表达式 System.Console 表示类型,而不是值。

  • x[1] = 123; 中,x[1] 表示一个变量 或一个 set 访问器,不是

  • 在 C# 7 中,您现在可以在需要表达式但没有值的上下文中使用 throw

等等。有许多表达式不表示值;你能想到更多吗?

Albahari 只是出于教学目的进行了简化。有关如何计算表达式的确切定义,请阅读规范。

Since a void expression has no value, is a void expression a statement?

没有。

Is a call to a method always an expression, whose value is the value returned by the method?

是的,假设该方法返回一个值。

练习:列出方法调用可以返回值的所有方式。

is a void expression always an invocation of a method which returns void?

是的。

What is the difference between " a method which doesn't return a value" and " a method which returns void"?

返回 void 的方法不返回值。但是有一些不返回值的非 void 方法。你能写一个吗?

"a call to method which doesn't return a value isn't an expression"

这个说法是错误的。所有方法调用都是表达式,无论它们是否返回值。

a call to a method which returns void is a void expression

没错。

您真的应该阅读规范。它是精确的。

关于c# - void 表达式没有值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234246/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com