在 C# 代码中使用 using 块有什么意义?

发布于 2024-07-15 09:01:17 字数 236 浏览 4 评论 0原文

我看到大量具有以下语法的代码片段,

using (RandomType variable = new RandomType(1,2,3))
{
   // A bunch of code here.

}

为什么不直接声明变量并使用它呢?

这种使用语法似乎只会使代码变得混乱并使其可读性降低。 如果该变量如此重要以至于只能在该范围内使用,为什么不将该块放入函数中呢?

I see loads of code snippets with the following Syntax

using (RandomType variable = new RandomType(1,2,3))
{
   // A bunch of code here.

}

why not just declare the variable and use it?

This Using syntax seems to just clutter the code and make it less readable. And if it's so important that that varible is only available in that scope why not just put that block in a function?

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

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

发布评论

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

评论(6

一袭白衣梦中忆 2024-07-22 09:01:17

使用有一个非常明确的目的。

它设计用于与实现 IDisposable 的类型一起使用。

在您的情况下,如果 RandomType 实现 IDisposable,它将在块末尾得到 .Dispose()'d。

Using has a very distinct purpose.

It is designed for use with types that implement IDisposable.

In your case, if RandomType implements IDisposable, it will get .Dispose()'d at the end of the block.

寒尘 2024-07-22 09:01:17
using (RandomType variable = new RandomType(1,2,3))
{
   // A bunch of code here.
}

与以下内容几乎相同(有一些细微的差异):

RandomType variable = new RandomType(1,2,3);

try
{
    // A bunch of code
}
finally
{
    if(variable != null)
        variable.Dispose()
}

请注意,在调用“Using”时,您可以将任何内容强制转换为 IDisposable:

using(RandomType as IDisposable)

finally 中的 null 检查将捕获任何实际上未实现 IDisposable 的内容。

using (RandomType variable = new RandomType(1,2,3))
{
   // A bunch of code here.
}

is pretty much the same (with a few subtle differences) as:

RandomType variable = new RandomType(1,2,3);

try
{
    // A bunch of code
}
finally
{
    if(variable != null)
        variable.Dispose()
}

Note that when calling "Using", you can cast anything as IDisposable:

using(RandomType as IDisposable)

The null check in the finally will catch anything that doesn't actually implement IDisposable.

蓝眸 2024-07-22 09:01:17

不,它不会使您的代码变得混乱,或使其可读性降低。

using 语句只能用于 IDisposable 类型(即实现 IDisposable 的类型)。

通过在 using 块中使用该类型,当 using 块的范围结束时,将使用该类型的 Dispose 方法。

因此,请告诉我哪些代码对您来说可读性较差:

using( SomeType t = new SomeType() )
{
   // do some stuff
}

或者

SomeType t = new SomeType();

try
{
   // do some stuff
}
finally
{
   if( t != null ) 
   {
      t.Dispose();
   }
}

No, it does not clutter your code , or make it less readable.

A using statement can only be used on IDisposable types (that is, types that implement IDisposable).

By using that type in a using - block, the Dispose method of that type will be used when the scope of the using-block ends.

So, tell me which code is less readable for you:

using( SomeType t = new SomeType() )
{
   // do some stuff
}

or

SomeType t = new SomeType();

try
{
   // do some stuff
}
finally
{
   if( t != null ) 
   {
      t.Dispose();
   }
}
梦里寻她 2024-07-22 09:01:17

using 语句中使用的对象必须实现 IDisposable,因此在作用域结束时,可以保证调用 Dispose(),因此理论上,您的对象应该在此时被释放。 在某些情况下,我发现它使我的代码更加清晰。

An object being used in a using statement must implement IDisposable, so at the end of the scope, you're guaranteed that Dispose() will be called, so theoretically, your object should be released at that point. In some cases, I've found it makes my code clearer.

坠似风落 2024-07-22 09:01:17

using 关键字提供了一种确定性的方法来清理对象分配的托管或非托管资源。 如果不使用 using 关键字,则在完成该对象后,您有责任调用 Dispose() (或在某些情况下,调用 Close())。 否则,资源可能直到下一次垃圾回收才被清理干净,甚至根本没有清理干净。

The using keyword provides a deterministic way to clean up the managed or unmanaged resources that an object allocates. If you don't use the using keyword, you are responsible to call Dispose() (or in some cases, Close()) when finished with that object. Otherwise, the resources may not be cleaned up until the next garbage collection, or even not at all.

獨角戲 2024-07-22 09:01:17

根据MSDN,以下使用 code:

using (Font font1 = new Font("Arial", 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}

扩展为:

{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}

它确实不会让你的代码变得混乱。 实际上恰恰相反!

According to MSDN, the following using code:

using (Font font1 = new Font("Arial", 10.0f)) 
{
    byte charset = font1.GdiCharSet;
}

expands to this:

{
  Font font1 = new Font("Arial", 10.0f);
  try
  {
    byte charset = font1.GdiCharSet;
  }
  finally
  {
    if (font1 != null)
      ((IDisposable)font1).Dispose();
  }
}

And it does really not clutter your code. Quite the opposite actually!

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