本文介绍了如果空"如果"声明在C#中导致错误或警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我从一个活生生的例子入手:

Let me start from a real life example:

客户:  亚历克斯,只是发现了一些在RemovalProcessor怪行138:

if (Session.Handler.ExecutePrefetchTasks()==null);
  Session.ExecuteDelayedQueries();

    

如若背后的'如果'在那里的semicolumn?

Should the semicolumn behind the 'if' be there?

我:  哎呀......我会送这个给我们的人来检查,但最有可能的,你是对的。

Me: Oops... I'll send this to our guys to check, but most likely, you're right.

虽然此案是罕见的,我承认几乎所有的大项目也有类似的问题。

Although the case is rare, I admit nearly any big project has similar issue.

据我所知,分号(和语句块),在C#中使用规则不能被改变(我个人倒preFER Python的风格)。但我认为这是一个好主意,以确定的正是这一点的情况下使用如果语句,它作为一个错误或警告分类。

I understand that semicolon (and statement block) usage rules in C# can't be changed (personally I'd prefer Python style). But I think it's a good idea to identify exactly this case with if statement, and classify it as an error or warning.

几个Q / A我心里有:

Few Q/A I have in mind:

  • 为什么警告或错误应该在这种情况下产生的?

由于这是一个开发者的失误与可能是99%的概率。

Because it's a developer's mistake with may be 99% probability.

为什么错误是preferable在这种情况下?

在许多情况下,警告开发人员忽略。

In many cases warnings are ignored by developers.

我理解这是他们自己的问题,并有与/ warnaserror(威胁警告视为错误)开关,但由于这是一个错误具有非常高的概率,并且,如果它不是错误(真;?)),这是很容易解决这个问题,可能是更好的这种情况进行分类为错误。

I understand this is their own problem, and there is /warnaserror(threat warnings as errors) switch, but since this is an errorwith very high probability, and, if it isn't an error (really? ;) ),it's quite easy to fix this, may be it's better to classify this caseas an error.

最后,在这种情况下,错误将不会限制的开发商,因为这样的code能(和可能,必须)总是被改写没有如果语句。

Finally, error in this case won't "restrict" the developer,since such code can (and likely, must) always be rewrittenwithout if statement.

为什么警告preferable在这种情况下?

这不会打破兼容性;我也怀疑一些code发电机可能会产生code依赖于当前的行为。

This won't break the compatibility; I also suspect some code generatorsmay generate code relying on the current behavior.

所以,我会很高兴听到你对这个观点。

So I'd be glad to hear your opinions about this.

推荐答案

这已经产生了警告:

可能误以为空语句

我同意你的错误将是preferable(如果你真的想要一个空语句,总可以写成 {} ,这是更显式) - 但他们不打算改变C#语言以这种方式。这将是一个重大更改,我怀疑他们(读:埃里克利珀的)理由将受益不大于成本

I agree with you that an error would have been preferable (if you really want an empty statement, can always write it as { }, which is more explicit) — but they are not going to change the C# language in this way. It would be a breaking change, and I suspect their (read: Eric Lippert’s) justification will be "the benefit doesn’t outweigh the cost".

这篇关于如果空"如果"声明在C#中导致错误或警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 01:11