天天看点

C#中“使用”的用途是什么

本文翻译自:What are the uses of “using” in C#

User kokos answered the wonderful Hidden Features of C# question by mentioning the

using

keyword.

用户kokos通过提及

using

关键字,回答了C#问题的精彩“ 隐藏功能” 。

Can you elaborate on that?

您能详细说明一下吗?

What are the uses of

using

?

使用有什么

using

#1楼

参考:https://stackoom.com/question/Jc9/C-中-使用-的用途是什么

#2楼

public class ClassA:IDisposable

{
   #region IDisposable Members        
    public void Dispose()
    {            
        GC.SuppressFinalize(this);
    }
    #endregion
}
           
public void fn_Data()

    {
     using (ClassA ObjectName = new ClassA())
            {
                //use objectName 
            }
    }
           

#3楼

Using Clause is used to define the scope for the particular variable.

使用子句用于定义特定变量的范围。

For example:

例如:
Using(SqlConnection conn=new SqlConnection(ConnectionString)
            {
                Conn.Open()
            // Execute sql statements here.
           // You do not have to close the connection explicitly here as "USING" will close the connection once the object Conn becomes out of the defined scope.
            }
           

#4楼

using, in the sense of

在某种意义上使用
using (var foo = new Bar())
{
  Baz();
}
           

Is actually shorthand for a try/finally block.

实际上是try / finally块的简写。

It is equivalent to the code:

它等效于代码:
var foo = new Bar();
try
{
  Baz();
}
finally
{
  foo.Dispose();
}
           

You'll note, of course, that the first snippet is much more concise than the second and also that there are many kinds of things that you might want to do as cleanup even if an exception is thrown.

您当然会注意到,第一个代码段比第二个代码段要简洁得多,而且即使抛出异常,您也可能想做许多事情来进行清理。

Because of this, we've come up with a class that we call Scope that allows you to execute arbitrary code in the Dispose method.

因此,我们提出了一个称为Scope的类,该类使您可以在Dispose方法中执行任意代码。

So, for example, if you had a property called IsWorking that you always wanted to set to false after trying to perform an operation, you'd do it like this:

因此,例如,如果您有一个名为IsWorking的属性,在尝试执行某个操作后始终希望将其设置为false,则可以这样做:
using (new Scope(() => IsWorking = false))
{
  IsWorking = true;
  MundaneYetDangerousWork();
}
           

You can read more about our solution and how we derived it here .

您可以在此处阅读有关我们的解决方案以及如何派生更多信息的信息 。

#5楼

Everything outside the curly brackets is disposed, so it is great to dispose your objects if you are not using them.

大括号之外的所有内容都已处置,因此如果不使用它们,最好将其处置。

This is so because if you have a SqlDataAdapter object and you are using it only once in the application life cycle and you are filling just one dataset and you don't need it anymore, you can use the code:

之所以这样,是因为如果您有一个SqlDataAdapter对象,并且在应用程序生命周期中仅使用了一次,并且仅填充了一个数据集,而不再需要它,则可以使用以下代码:
using(SqlDataAdapter adapter_object = new SqlDataAdapter(sql_command_parameter))
{
   // do stuff
} // here adapter_object is disposed automatically
           

#6楼

You can make use of the alias namespace by way of the following example:

您可以通过以下示例使用别名名称空间:
using LegacyEntities = CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects;
           

This is called a using alias directive as as you can see, it can be used to hide long-winded references should you want to make it obvious in your code what you are referring to eg

如您所见,这被称为using别名指令 ,如果您想在代码中使其变得明显,则可以将其用于隐藏长引用,例如
LegacyEntities.Account
           

instead of

代替
CompanyFoo.CoreLib.x86.VBComponents.CompanyObjects.Account
           

or simply

或简单地
Account   // It is not obvious this is a legacy entity