# "基于模式使用" 和 "using 声明""pattern-based using" and "using declarations"

## 动机Motivation

using 语句是当今资源管理的有效工具，但它需要很多工作。The using statement is an effective tool for resource management today but it requires quite a bit of ceremony. 具有多个要管理的资源的方法可以从语法上陷入一系列 using 语句。Methods that have a number of resources to manage can get syntactically bogged down with a series of using statements. 这种语法负担足以表明，大多数编码样式准则对此方案在大括号附近有一个例外。This syntax burden is enough that most coding style guidelines explicitly have an exception around braces for this scenario.

using 声明消除了这里的许多仪式，并与包含资源管理块的其他语言一起获取了 c #。The using declaration removes much of the ceremony here and gets C# on par with other languages that include resource management blocks. 此外，基于模式的 using 允许开发人员展开可参与的类型集。Additionally the pattern-based using lets developers expand the set of types that can participate here. 在许多情况下，无需创建仅存在的包装类型，就可以在语句中使用值 usingIn many cases removing the need to create wrapper types that only exist to allow for a values use in a using statement.

## 详细设计Detailed Design

### using 声明using declaration

if (...)
{
using FileStream f = new FileStream(@"C:\users\jaredpar\using.md");
// statements
}

// Equivalent to
if (...)
{
using (FileStream f = new FileStream(@"C:\users\jaredpar\using.md"))
{
// statements
}
}


{
using var f1 = new FileStream("...");
using var f2 = new FileStream("..."), f3 = new FileStream("...");
...
// Dispose f3
// Dispose f2
// Dispose f1
}


goto在声明中没有围绕或任何其他控制流构造的限制 usingThere are no restrictions around goto, or any other control flow construct in the face of a using declaration. 相反，代码的行为与对等效语句的行为一样 usingInstead the code acts just as it would for the equivalent using statement:

{
using var f1 = new FileStream("...");
target:
using var f2 = new FileStream("...");
if (someCondition)
{
// Causes f2 to be disposed but has no effect on f1
goto target;
}
}


local-using-declaration:
using type using-declarators

using-declarators:
using-declarator
using-declarators , using-declarator

using-declarator:
identifier = expression


• 不能直接显示在标签 case 内，而是必须位于标签内的 块 case 内。May not appear directly inside a case label but instead must be within a block inside the case label.
• 可能不会显示为变量声明的 out 一部分。May not appear as part of an out variable declaration.
• 每个声明符必须具有初始值设置项。Must have an initializer for each declarator.
• 本地类型必须可隐式转换为 IDisposable 或满足 using 模式。The local type must be implicitly convertible to IDisposable or fulfill the using pattern.

### 使用基于模式的pattern-based using

class Resource
{
public void Dispose() { ... }
}

using (var r = new Resource())
{
// statements
}


• ref struct：这些类型目前无法实现接口，因此不能参与 using 语句。ref struct: These types can't implement interfaces today and hence can't participate in using statements.
• 扩展方法将使开发人员能够增强其他程序集中的类型以参与 using 语句。Extension methods will allow developers to augment types in other assemblies to participate in using statements.

{
Resource r = new Resource();
try {
// statements
}
finally {
if (r != null) r.Dispose();
}
}


## 未来扩展Future Expansions

### 固定局部变量fixed locals

fixed语句包含的所有语句属性都可以 using 具有 using 局部变量。A fixed statement has all of the properties of using statements that motivated the ability to have using locals. 应考虑将此功能扩展到 fixed 局部变量。Consideration should be given to extending this feature to fixed locals as well. 生存期和顺序规则应该同样适用于 usingfixed 此处。The lifetime and ordering rules should apply equally well for using and fixed here.