一般 C# 问题

发布于 2024-12-04 10:53:37 字数 339 浏览 0 评论 0原文

下面的类有两个方法,其中 M1 抱怨“并非所有代码路径都返回值”,而 M2 则没有。

问题:编译器如何在返回值上下文中解析 M2 ? NotImplementedException 实例如何隐式转换为 int (如果有任何内部编译时解析)

class A
        {
            int M1()
            {
            }
            int M2()
            {
                throw new NotImplementedException();
            }
        }

Following class has two method wherein M1 complain ‘not all code path return a value’ and M2 doesn’t.

Question: How does the compiler resolve M2 in context of return value ? How NotImplementedException instance is implicitly casted as int (if there is any internal compile time resolution)

class A
        {
            int M1()
            {
            }
            int M2()
            {
                throw new NotImplementedException();
            }
        }

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

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

发布评论

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

评论(4

不及他 2024-12-11 10:53:37

方法并不总是需要返回值;特别是,还允许通过抛出异常来退出(在这种情况下不返回任何值)。

编辑:具体来说,规则对于返回 int 的方法体,方法如下:

  1. 方法中的所有 return 语句必须返回可转换为 int
  2. 表达式方法块的末尾必须不可到达

在您的示例中,编译器可以证明 M2 始终通过抛出异常退出,因此方法块的末尾不可到达(满足规则 #2)。也没有 return 语句,这也满足规则 #1。因此这是一个有效的方法定义。

另一方面,M1 不满足规则#2,因此它不合法。

您可能会被根本没有提到抛出的错误消息误导,但请考虑在几乎所有情况下具有返回值的方法都会返回而不是抛出 - 编译器只是告诉您想要您可能忘记了去做。

A method is not always required to return a value; in particular, it is also allowed to exit by throwing an exception (in which case no value is returned).

Edit: Specifically, the rules for the body of a method that returns int are:

  1. All return statements in the method must return an expression convertible to int
  2. The end of the method block must not be reachable

In your example, the compiler can prove that M2 always exits by throwing, so the end of the method block is not reachable (satisfies rule #2). There are also no return statements, which also satisfies rule #1. Hence this is a valid method definition.

On the other hand, M1 does not satisfy rule #2 so it is not legal.

You are probably misled by the error message which does not mention throwing at all, but consider that in almost all cases methods with return values do return instead of throwing -- the compiler just tells you want you probably forgot to do.

盗心人 2024-12-11 10:53:37

异常会影响代码的流程。 throw 之后的任何语句都不会被执行,编译器可以证明这一点,因此对通过该方法的路径感到满意。

该异常不会导致返回 int,正常意义上不会返回任何内容。相反,会生成异常,CLR 会以不同的方式处理这些异常。

http://msdn.microsoft.com/en-我们/library/ms173160(v=vs.80).aspx

Exceptions affect the flow of the code. Any statements after the throw would not be executed, the compiler can prove this so is happy with the path through the method.

The exception would not result in an int being returned, nothing would be returned in the normal sense. Instead an exception is generated, the CLR handles these differently.

http://msdn.microsoft.com/en-us/library/ms173160(v=vs.80).aspx

不交电费瞎发啥光 2024-12-11 10:53:37

该异常不会被转换为 int。编译器知道这是一个总会遇到的异常,因此不会抱怨。当发生异常时,它会将堆栈展开到异常处理块或崩溃。 int 永远不会返回给方法调用者。

The exception won't be cast as an int. The compiler knows that it is an exception that will always be reached so doesn't complain. When the exception is hit it will unwind the stack to an exception handling blocks or crash. An int will never be returned to the method caller.

泪之魂 2024-12-11 10:53:37

MSDN,

throw 语句用于在程序执行过程中发出异常情况(异常)发生的信号。

当代码执行遇到 throw 语句时,程序将停止并向用户显示异常消息(如果程序员没有指定任何错误处理逻辑)

As stated in MSDN,

The throw statement is used to signal the occurrence of an anomalous situation (exception) during the program execution.

When code execution run into throw statement, program is stopped and an exception message is shown to an user (if a programmer didn't specify any error handling logic)

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