Floating curly braces in C#

前端 未结 6 1042
执念已碎
执念已碎 2020-12-03 11:42

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).

{
         


        
6条回答
  •  隐瞒了意图╮
    2020-12-03 12:16

    You can use an open and close set of curly braces to define a self containing block, which has its 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 its place.

提交回复
热议问题