哪个版本的 using 语句是正确的或最安全的?

发布于 2024-12-15 09:21:56 字数 955 浏览 0 评论 0原文

下面的“using”语句的版本 I 和 II 都有效,但我怀疑第一个版本之所以有效,是因为 Visual Studio 2010 中的 C# 垃圾收集器尚未删除变量“context”(实体框架变量)。另一方面,我从一个看似有信誉的来源从网上获得了第一个版本,所以我认为它可以吗?

版本 I:

try
{
    using ( AnEFEntity context = new AnEFEntity()) //note: no curly brackets!
    using (var ts = new System.Transactions.TransactionScope())
    {
        // stuff here that uses variable context
    }
}
catch (Exception ex)
{
}

// 上面的代码可以正常编译并正常工作,但是第一个“using”语句是否在范围内?看起来是这样,但又很可疑。

版本 II:

try
{

    using ( AnEFEntity context = new AnEFEntity())
    { //note a curly bracket used for first ‘using’
        using (var ts = new System.Transactions.TransactionScope())
        {
            // stuff here that uses variable context
        }
    } //note the placement of the second curly bracket for the first 
}
catch (Exception ex)
{
}

// 上面的代码也可以正常编译并正常工作——它比第一个版本更安全吗?

Both versions I and II of the 'using' statement below work, but I have a suspicion that the first version only works because the C# garbage collector in Visual Studio 2010 has not deleted the variable "context" (a Entity Framework variable). On the other hand, I got the first version from off the net from a seemingly reputable source, so I assume it's OK?

VERSION I:

try
{
    using ( AnEFEntity context = new AnEFEntity()) //note: no curly brackets!
    using (var ts = new System.Transactions.TransactionScope())
    {
        // stuff here that uses variable context
    }
}
catch (Exception ex)
{
}

// the above compiles fine and works fine—but is the first ‘using’ statement in scope? Seems so but it’s suspicious.

VERSION II:

try
{

    using ( AnEFEntity context = new AnEFEntity())
    { //note a curly bracket used for first ‘using’
        using (var ts = new System.Transactions.TransactionScope())
        {
            // stuff here that uses variable context
        }
    } //note the placement of the second curly bracket for the first 
}
catch (Exception ex)
{
}

// the above also compiles fine and works fine—is it more safe than the first version?

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

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

发布评论

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

评论(2

笨死的猪 2024-12-22 09:21:56

它对编译后的代码绝对没有区别,因为外部 using 语句的主体只是内部 using 语句。就我个人而言,我通常更喜欢放入大括号,因为如果您想在外部 using 语句的开头和内部 <代码>using语句。然而,缩进也可以使这一点更加清晰。您问题中的代码很难理解,因为它根本没有缩进,而我会使用如下两种格式:

using (...)
using (...)
{
    // Body
}

vs

using (...)
{
    using (...)
    {
        // Body
    }
}

单大括号版本的风险是您最终会意外编写此代码:

using (...)
    Log("Hello");
using (...)
{
    // Body
}

此时代码就执行流程而言不再执行您想要的操作。这通常会导致编译时错误,因为第二个using语句通常依赖于第一个语句中声明的资源,但并非总是如此。

It makes absolutely no difference to the compiled code, because the body of the outer using statement is just inner using statement. Personally I usually prefer to put the braces in, because then it's clearer what's going on if you want to introduce more code between the start of the outer using statement and the inner using statement. However, indentation can make this clearer as well. The code in your question is hard to follow because it's not indented at all, whereas I'd use the two formats like this:

using (...)
using (...)
{
    // Body
}

vs

using (...)
{
    using (...)
    {
        // Body
    }
}

The risk of the single-brace version is that you end up writing this accidentally:

using (...)
    Log("Hello");
using (...)
{
    // Body
}

At that point the code no longer does what you want in terms of execution flow. This will usually lead to a compile-time error because the second using statement would normally depend on the resource declared in the first, but not always.

初见 2024-12-22 09:21:56

这里也有同样的效果,如果存在多行,则使用大括号,如果只有一行,则不需要,但为了可读性,您可以使用它

                bool firstcondition = true;
                bool secondcondtion = true;
                if (firstcondition)
                    if (secondcondtion)
                    {
                        MessageBox.Show("inside");
                        MessageBox.Show("inside");
                    }

same effect here also,Braces are used if mutiple lines are there, if only a single line then no need of that but for readability you can use it

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