C# using 语句可以不加花括号吗?

发布于 2024-09-15 11:57:00 字数 395 浏览 8 评论 0原文

我今天浏览同事的 C# 代码,发现了以下内容:

    using (MemoryStream data1 = new MemoryStream())
    using (MemoryStream data2 = new MemoryStream())
    {
        // Lots of code..........
     }

我总是看到 using 语句后面跟着一对定义对象生命周期的大括号。我编写代码的同事说,不需要 data1 using 语句的大括号,代码执行的操作与它们存在并嵌套 < code>data2 using 语句。那么,当省略花括号时会发生什么呢?

I was browsing a coworkers c# code today and found the following:

    using (MemoryStream data1 = new MemoryStream())
    using (MemoryStream data2 = new MemoryStream())
    {
        // Lots of code..........
     }

I had always seen the using statement followed by a pair of curly braces that defined the scope of the object life. My coworker who wrote the code said that the curly braces for the data1 using statement weren't needed and the code did the same thing as if they were present and nested the data2 using statement. So, what happens when the curly braces are ommitted?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

静谧 2024-09-22 11:57:00

是的,您也可以将它们放在一个 using 语句中:

using (MemoryStream data1 = new MemoryStream(), 
                    data2 = new MemoryStream())
{
    // do stuff
}

Yes, you can also put them in one using statement:

using (MemoryStream data1 = new MemoryStream(), 
                    data2 = new MemoryStream())
{
    // do stuff
}
百合的盛世恋 2024-09-22 11:57:00

从 C# 8 开始,您还可以使用不带任何大括号的语法:

using var foo = new Foo();
var bar = foo.Bar();

foo 将在其作用域的末尾(通常在方法的末尾)进行处理 - 因此请注意 & 的位置。何时使用。

参考 - https://learn.microsoft .com/en-us/dotnet/csharp/language-reference/keywords/using-statement#example

From C# 8 you can also use this syntax without any braces:

using var foo = new Foo();
var bar = foo.Bar();

foo will then be disposed at end of its scope (usually at the end of a method) - so be mindful where & when to use.

Reference - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement#example

金橙橙 2024-09-22 11:57:00

当您在 forif 语句中省略大括号时,同样的规则也适用。

顺便说一句,如果您反映到已编译的代码中,编译器反编译器会添加大括号。

The same rules apply when you omit the curly braces in a for or an if statement.

Incidentally if you reflect into the compiled code, the compiler decompiler adds the braces.

在巴黎塔顶看东京樱花 2024-09-22 11:57:00

正是他所说的。上面的代码与这样的写法完全一样:

using (MemoryStream data1 = new MemoryStream()) 
{
    using (MemoryStream data2 = new MemoryStream())
    {
        // Lots of code
    }
}

if/else/for/while/using/etc 语句后可以省略大括号,只要该语句中只有一个命令即可。示例:

// Equivalent!
if (x==6) 
    str = "x is 6";

if(x == 6) {
    str = "x is 6";
}

// Equivalent!
for (int x = 0; x < 10; ++x) z.doStuff();

for (int x = 0; x < 10; ++x) {
    z.doStuff();
}

// NOT Equivalent! (The first one ONLY wraps the p = "bob";!)
if (x == 5) 
p = "bob";
z.doStuff();

if (x == 5) {
   p = "bob";
   z.doStuff();
}

Exactly what he said. The code above is exactly the same as writing:

using (MemoryStream data1 = new MemoryStream()) 
{
    using (MemoryStream data2 = new MemoryStream())
    {
        // Lots of code
    }
}

You can omit the curly braces after an if/else/for/while/using/etc statement as long as there is only one command within the statement. Examples:

// Equivalent!
if (x==6) 
    str = "x is 6";

if(x == 6) {
    str = "x is 6";
}

// Equivalent!
for (int x = 0; x < 10; ++x) z.doStuff();

for (int x = 0; x < 10; ++x) {
    z.doStuff();
}

// NOT Equivalent! (The first one ONLY wraps the p = "bob";!)
if (x == 5) 
p = "bob";
z.doStuff();

if (x == 5) {
   p = "bob";
   z.doStuff();
}
生生不灭 2024-09-22 11:57:00

这是可行的,但有风险,因为如果有人后来决定要在其他事情发生之前对 data1 执行某些操作,他们可能会将其放在 data1 使用之后,这会将其排除在 data2 使用的整个范围之外。这可能会破坏编译,但仍然是一个有风险且毫无意义的语法快捷方式。

This is viable but risky, because if somebody later decides they want to do something to data1 before other stuff happens to it, they might place it right after the data1's using, which would take it out of the entire scope of data2's using. This would likely break compilation but still is a risky and pointless syntax shortcut..

南渊 2024-09-22 11:57:00

正如您的同事所说,这相当于嵌套语句。 data2 的 dispose 将在 data1 的 dispose 函数之前调用。

Exactly what your colleague said, that is the equivalent of nesting the statements. The dispose for data2 would be called immediately before the dispose function for data1.

奢望 2024-09-22 11:57:00

正如人们所说:鉴于语句后面只有一行,没有花括号也可以工作。然而,人们忽略了在他们的示例中表明这一行可以是带有自己的大括号的 if/using/for 。这是一个例子:

if(foo)
  if(bar)
  {
     doStuff();
  }

As people have said: given there only being one line following a statement it will work without the curled braces. However, people are neglecting to show in their examples that that one line can be an if/using/for with it's own curled braces. Here is an example:

if(foo)
  if(bar)
  {
     doStuff();
  }
江挽川 2024-09-22 11:57:00

如果该语句后面只有一条指令,则不需要花括号。它就像 if 语句一样。

if(true)
{
   Console.Writeline("hello")
}

意思是一样的

if(true)
   Console.Writeline("hello")

If there is only one instruction which follow the statement, the bracets are not needed. It is just like with if statement.

if(true)
{
   Console.Writeline("hello")
}

means the same that

if(true)
   Console.Writeline("hello")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文