天天看點

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