当你使用范围,而不在C#中的声明? [英] When do you use scope without a statement in C#?

查看:172
本文介绍了当你使用范围,而不在C#中的声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在最近,我发现你可以在C#中做到这一点:

Just recently I found out you can do this in C#:

{
    // google
    string url = "#";

    if ( value > 5 )
        url = "http://google.com";

    menu.Add( new MenuItem(url) );
}
{
    // cheese
    string url = "#"; // url has to be redefined again, 
                      // so it can't accidently leak into the new menu item

    if ( value > 45 )
        url = "http://cheese.com";

    menu.Add( new MenuItem(url) );
}



代替,即:

instead of i.e.:

    string url = "#";

    // google
    if ( value > 5 )
        url = "http://google.com";

    menu.Add( new MenuItem(url) );


    // cheese
    url = "#"; // now I need to remember to reset the url

    if ( value > 45 )
        url = "http://cheese.com";

    menu.Add( new MenuItem(url) );

这可能是一个坏榜样,可以在很多其他方式来解决。

This might be a bad example that can be solved in a lot of other manners.

是否有任何的图案,其中范围不言功能的的一个好的做法呢?

Are there any patterns where the 'scope without statement' feature is a good practice?

推荐答案

如果你问他们为什么要实现该功能?

If you ask why did they implement the feature?

我已经开发了我自己的语言,和我有什么发现是,没有一个报表范围'语法悄悄成语法,很容易。 这是一个副作用,如果你愿意,写一个语法,只是很好地工作和放大器时; ,很容易阅读

I've developed my own language, and what I found was that the 'scope without a statement' syntax creeps up into grammar, very easily. It's a side-effect if you will, when writing a grammar that just works nicely & is easy to read.

在我的语言,我也有同样的功能,我从来没有专为它明确 - 我得到了它免费。我从来没有坐在我的办公桌上,心里想:哦,是不是很酷有这样的'功能'?。哎呀 - 起初,我甚至不知道我的语法允许的。

In my language, I have the same functionality, and I never "designed" for it explicitly - I got it for free. I never sat down on my desk, and thought "oh, wouldn't it be cool to have such 'feature'?". Heck - at first, I didn't even know that my grammar allowed that.

{}是一个复合语句,因为这简化了$语法B $ B中的所有你想要的地方使用(条件语句,循环体,
等)......因为,让你离开了括号时被控制单个
语句('如果(一个小于10)++中的;等)。

'{}' is a "compound statement" because that simplifies the syntax for all the places you'd want to use it (conditionals, loop bodies, etc)... and because that lets you leave out the braces when a single statement is being controlled ('if (a<10) ++a;' and the like).

,它可以在任何地方发表声明直接使用会出现跌倒
出来的事实;其他
答案都表示这是无害的,偶尔也有帮助。 如果不破,不解决它。 - keshlam

The fact that it can be used anywhere a statement can appear falls directly out of that; it's harmless, and occasionally helpful as other answers have said. "If it ain't broke, don't fix it. - keshlam.

所以,问题不是这么多为什么他们实现它,而是为什么他们不禁止它/他们为什么允许这样做?

So, the question is not so much about "why did they implement it", but rather, "why didn't they ban it / why did they allow this?"

我可以在我的语言班这一特定功能?当然,但我看不出有任何理由去 - 这将是该公司支付额外的费用。

Could I ban this specific functionality in my language? Sure, but I see no reason to - it'll be extra cost for the company.

现在,上面的故事可能或可能不是C#真的,但我没有设计语言(我也不是一个真正的语言设计者),所以很难说究竟为什么,但想到我反正提到它。

Now, the above story might, or might not be true for C#, but I didn't design language (nor am I really a language designer), so it's hard to say why exactly, but thought I'd mention it anyway.

相同的功能是在C ++中,这实际上有一些用例 - 它允许一个对象的一个​​终结被确定性地运行,如果该对象超出范围,虽然这不是对C#的情况

The same functionality is in C++, which actually has some use cases - it allows a finalizer of an object to be run deterministically, if the object goes out of the scope, though this is not the case for C#.

这是说,我没有使用过,在我4岁的C#特定的语法(嵌入语句 - >块谈到具体的termins时),无论是我见过它被任何地方使用。您的例子是乞讨被重构为方法。

That said, I have not used that specific syntax in my 4 years of C# (embedded-statement -> block, when talking about concrete termins), neither I've seen it being used anywhere. Your example is begging to be refactored to methods.

看看C#的语法:的 http://msdn.microsoft.com/en-us/library/aa664812%28v=vs.71%29.aspx

另外,叶普说,我用'{}'语法,以确保在'开关'建设每种情况下块具有单独的本地范围:

Also, as Jeppe has said, I've used the '{}' syntax in order to make sure that each case-block in 'switch' construction has separate local scope:

int a = 10;
switch(a)
{
    case 1:
    {
        int b = 10;
        break;
    }

    case 2:
    {
        int b = 10;
        break;
    }
}

这篇关于当你使用范围,而不在C#中的声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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