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

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

问题描述

我遇到这样一种情况:非void方法缺少的收益语句和code编译依旧。
我知道,while循环后声明的无法访问(死code)和永远不会被执行。但为什么不编译器甚至警告返回的东西吗?或者为什么会一门语言让我们拥有具有无限循环,并没有返回任何一个非空的方法?

 公众诠释doNotReturnAnything(){
    而(真){
        //做一点事
    }
    //没有return语句
}

如果我在while循环中增加break语句(甚至是有条件的),编译器臭名昭著的错误的抱怨:方法不返回一个值'(的Eclipse)和并非所有code路径返回值(Visual Studio中)

 公众诠释doNotReturnAnything(){
    而(真){
        如果(mustReturn)破;
        //做一点事
    }
    //没有return语句
}

这是Java和真正的C#


解决方案

  

为什么一个语言让我们拥有具有无限循环,并没有返回任何一个非空的方法?


对于非void方法的规则的返回每code路径必须返回一个值的,而规则是在你的程序满足:零出零code路径的返回不返回一个值。该规则是不是逢非void方法必须有一个返回code路径。

这使您可以编写存根方法,如:

 的IEnumerator IEnumerable.GetEnumerator()
{
    抛出新NotImplementedException();
}

这是一个非空的方法。它的的是为了满足该接口一个非void方法。但似乎很傻,使这个非法的实现,因为它不返回任何东西。

这是你的方法,因为一个不可达的终点转到(记住,一个,而(真)是只是一个更愉快的方式来写转到)而不是(这是转到)是不相关的。


  

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


由于编译器没有很好的证据证明code是错误的。有人写了,而(真)键,它很可能是谁做的人知道他们在做什么。


  

我在哪里可以在C#中阅读更多关于可达性分析?


请参阅关于这个问题我的文章,在这里:

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

和你也可以考虑阅读C#规范。

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
}

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'(Eclipse) and 'Not all code paths return a value'(Visual Studio)

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

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?

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.

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?

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.

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

See my articles on the subject, here:

ATBG: de facto and de jure reachability

And you might also consider reading the C# specification.

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

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