非 void 方法中缺少 return 语句编译 [英] Missing return statement in a non-void method compiles

查看:35
本文介绍了非 void 方法中缺少 return 语句编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这样一种情况:非 void 方法 缺少 return 语句并且代码仍然可以编译.我知道 while 循环之后的语句无法访问(死代码)并且永远不会被执行.但是为什么编译器甚至不警告返回某些东西呢?或者为什么一种语言会允许我们拥有一个具有无限循环且不返回任何内容的非空方法?

I encountered a situation where a non-void method is missing a return statement and the code still compiles. I know that the statements after the while loop are unreachable (dead code) and would never be executed. But why doesn't the compiler even warn about returning something? Or why would a language allow us to have a non-void method having an infinite loop and not returning anything?

public int doNotReturnAnything() {
    while(true) {
        //do something
    }
    //no return statement
}

如果我在 while 循环中添加 break 语句(甚至是条件语句),编译器会抱怨臭名昭著的错误:Method does not return a value in Eclipse 和 Not all code路径在 Visual Studio 中返回一个值.

If I add a break statement (even a conditional one) in the while loop, the compiler complains of the infamous errors: Method does not return a value in Eclipse and Not all code paths return a value in Visual Studio.

public int doNotReturnAnything() {
    while(true) {
        if(mustReturn) break;
        //do something
    }
    //no return statement
}

Java 和 C# 都是如此.

This is true of both Java and C#.

推荐答案

为什么一种语言会允许我们拥有一个具有无限循环且不返回任何内容的非空方法?

Why would a language allow us to have a non-void method having an infinite loop and not returning anything?

非空方法的规则是每个返回的代码路径都必须返回一个值,并且该规则在您的程序中得到满足:返回的零代码路径确实返回一个值.规则不是每个非 void 方法都必须有一个返回的代码路径".

The rule for non-void methods is every code path that returns must return a value, and that rule is satisfied in your program: zero out of zero code paths that return do return a value. The rule is not "every non-void method must have a code path that returns".

这使您能够编写存根方法,例如:

This enables you to write stub-methods like:

IEnumerator IEnumerable.GetEnumerator() 
{ 
    throw new NotImplementedException(); 
}

这是一个非空方法.它必须是一个非空方法以满足接口.但是让这个实现非法似乎很愚蠢,因为它不返回任何东西.

That's a non-void method. It has to be a non-void method in order to satisfy the interface. But it seems silly to make this implementation illegal because it does not return anything.

由于 goto,你的方法有一个无法到达的终点(记住,while(true) 只是一种更愉快的编写 goto) 而不是 throw(这是 goto 的另一种形式)不相关.

That your method has an unreachable end point because of a goto (remember, a while(true) is just a more pleasant way to write goto) instead of a throw (which is another form of goto) is not relevant.

为什么编译器甚至不警告返回某些东西?

Why doesn't the compiler even warn about returning something?

因为编译器没有很好的证据证明代码是错误的.有人写了 while(true) 并且似乎这样做的人知道他们在做什么.

Because the compiler has no good evidence that the code is wrong. Someone wrote while(true) and it seems likely that the person who did that knew what they were doing.

在哪里可以阅读有关 C# 可达性分析的更多信息?

Where can I read more about reachability analysis in C#?

在此处查看我关于该主题的文章:

See my articles on the subject, here:

ATBG:事实上和法律上的可达性

您也可以考虑阅读 C# 规范.

And you might also consider reading the C# specification.

这篇关于非 void 方法中缺少 return 语句编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆