漂浮在C#大括号 [英] Floating curly braces in C#

查看:105
本文介绍了漂浮在C#大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
你用花括号额外的作用域?

我遇到今天我从来没有见过一段C#代码运行。程序员定义只用花括号(没有如果,类,函数等)。

I ran across a piece of C# code today I had not seen before. The programmer defined a block of code using only curly braces (no if, class, function, etc).

{
    int i = 0;
}
i++; //compile error

有一个宗旨,以这个以外使代码看起来更有条理?它是好的,坏的,或任何的做法是使用这种浮动上下文?

Is there a purpose to this other than making the code look more organized? Is it good, bad, or whatever practice to use this "floating" contexts?

推荐答案

您可以使用一个开闭集大括号定义一个包含块的自我,它有它自己的范围。

You can use an open and close set of curly braces to define a self containing block, which has it's own scope.

这是通常不被视为良好的编程习惯,虽然。

This is generally not considered good programming practice, though.

通常,如果有人在做这样的事情,它可能是最好创建一个方法/函数在它的地方。

Usually if someone is doing something like this, it's probably better to create a method/function in it's place.

这篇关于漂浮在C#大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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