一般 C# 问题
下面的类有两个方法,其中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
方法并不总是需要返回值;特别是,还允许通过抛出异常来退出(在这种情况下不返回任何值)。
编辑:具体来说,规则对于返回
int
的方法体,方法如下:return
语句必须返回可转换为int
的在您的示例中,编译器可以证明
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:return
statements in the method must return an expression convertible toint
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 noreturn
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.异常会影响代码的流程。 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
该异常不会被转换为 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.
如MSDN,
当代码执行遇到 throw 语句时,程序将停止并向用户显示异常消息(如果程序员没有指定任何错误处理逻辑)
As stated in MSDN,
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)