C# Using

75 阅读1分钟

  1、给命名空间取别名:

命名空间取别名

2.可释放对象:

在using{}后,释放using()里的对象

也可以缩写

在碰到using外侧{}后释放

用来代替 Close()、Dispose()。

不使用using需要手动释放非托管资源

**非托管资源:**实现了Dispose接口的类(要是能GC掉,为啥还要实现Dispose)

    //     Provides a mechanism for releasing unmanaged resources.
    public interface IDisposable
    {
        //
        // 摘要:
        //     Performs application-defined tasks associated with freeing, releasing, or resetting
        //     unmanaged resources.
        void Dispose();
    }