是'使用'C#关键字?

时间:2010-10-03 10:30:00

标签: c# .net idisposable

如果某个对象在C#中实现IDisposable,则可以编写

using(DisposableFoo foo = new DisposableFoo())

我一直想知道为什么using看起来像C#关键字,但需要在.Net框架中定义的接口。 using是关键字(在此上下文中,显然是用于定义库导入),还是Microsoft在Visual Studio / .Net框架中重载了它?我不希望C#关键字依赖于库。

7 个答案:

答案 0 :(得分:8)

它是一个需要IDisposable的关键字构造,因为它作用于对象的Dispose方法。让关键字使用类型并不是太过分了。如果是关键字,则需要条件(IE:Boolean)类型。

答案 1 :(得分:7)

According to Microsoft这是一个关键字。

答案 2 :(得分:6)

是的,它是一个关键字。 C#语言依赖于实现许多关键字的框架。

依赖于框架的其他关键字是exmaple foreach(使用IEnumerable)和LINQ语法(当与LINQ to Objects一起使用时需要System.Linq库)。

即使是intstring等关键字也依赖于框架中的System.Int32System.String类型。

答案 3 :(得分:4)

C#与 fairy 紧密结合,并且语言规范使用 minimal 运行时功能。用于“使用”的IDis实际上非常严格,因为在大多数情况下(例如“foreach”,LINQ等),无论是否有特定的运行时实现都支持。

然而,由于IDisposable / using构造是.NET的关键,我怀疑你甚至可以在没有它的情况下制作一个最小的(但合法的)CLI,因此在语言规范中提及它并不是一个真正的问题。

与特定类型相关的其他事项:

  • 字符串连接(+ vs string.Concat)
  • 扩展方法
  • generic new()
  • [Serializable](映射到IL标志,而不是属性)
  • foreach(但可以在没有任何接口的情况下使用)
  • 核心基类型(枚举数组对象值类型委托)
  • 表达式树编译器(但在规范中没有描述)

答案 4 :(得分:2)

是的,.NET框架中的语言和支持程序集之间存在明确的耦合。到目前为止,最重要的是mscorlib.dll,它是System.IDisposable接口声明的主页。它非常重要,甚至没有在C#项目的References节点中列出。 C#编译器假定它总是需要它并且将自己找到它,除非你使用特殊的/ nostdlib编译选项。

需要它的句法元素的其他例子:

  • array [],需要System.Array
  • [attributes],需要System.Attribute
  • catch,需要System.Exception
  • 委托,需要System.MulticastDelegate
  • type ?,需要System.Nullable<>
  • params,需要System.ParamArrayAttribute
  • typeof,需要System.Type

mscorlib中所有基本类型及其相应声明之间存在耦合(如Int32,String,Object,ValueType等)。

后面的示例是Linq查询理解语法,需要System.Core.dll和动态关键字,需要Microsoft.CSharp.dll。这些例子并非详尽无遗。

这是非常正常的,即使C编译器也假设运行时库可以使用像memset()和ldiv()这样的原语。

答案 5 :(得分:1)

我认为使用是一个基本上可以创建代码的关键字

B b=new B();//can get resource outside using statement but will dispose inside of it
using(A a=new A(),b,C c=new C() //A,B,C are variable of types that implement IDisposable){
SomeCode//Some code to execute
}

B b= new B();
try{ 
A a= new A();
C c= new C();//B isn't here since

SomeCode//execute the code

}finallly{
a.Dispose();//The reason A,B,C need to implement IDisposable
b.Dispose();//is so the compiler can make sure that they can call Dispose()
c.Dispose();
}

使用IDisposable的重要部分是因为c#是强类型的,在幕后调用Dispose(),并且实现IDisposable告诉编译器它可以。

类似于foreach与IEnumerable和IEnumerator(调用GetEnumerator(),Current(),MoveNext()的方式类似,虽然我认为它不会对仅使用数组的集合类执行此操作)。

答案 6 :(得分:1)

using是一个C#关键字,用作处理IDisposible对象的语法糖。根据MSDN

  

using block定义一个范围,在该范围之外将放置一个或多个对象。

using语句允许程序员指定何时使用资源的对象应该释放它们。提供给using语句的对象必须实现IDisposable接口。此接口提供Dispose方法,该方法应释放对象的资源。

令人惊讶的是,甚至MSDN也没有说明这是如何在引擎盖下发生的。它只说对象必须实现IDisposable接口,它在实现接口的对象上提供Dispose方法。因此,要处置对象,需要在对象上调用Dispose方法,该方法将清理并释放对象使用的资源。

看看这个例子..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //Assume that Car is IDisposible.
            using (Car myCar = new Car(1))
            {
                myCar.Run();
            }
        }
    }
}

编译器转换为代码看起来像这样..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BlogSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            //Declare myCar object with FullName of the type as seen in IL.
            BlogSamples.Car myCar;

            //Instantiate the object by calling the constructor, matching the flow of IL.
            myCar = new Car(1);

            try
            {
                myCar.Run();
            }
            finally
            {
                if(myCar != null)
                    myCar.Dispose();
            }
        }
    }
}

为了理解使用模块的确切运作方式,我建议您阅读此blog-post

http://www.ruchitsurati.net/index.php/2010/07/28/understanding-%E2%80%98using%E2%80%99-block-in-c/

相关问题