ZetCode

C# using

最后修改于 2025 年 5 月 21 日

C# using 教程展示了如何在 C# 中使用 using 语句/指令。

using 关键字有三种不同的用途

using 语句定义了一个作用域,在该作用域结束时,对象将被释放。 using 指令创建一个命名空间的别名或导入其他命名空间中定义的类型。 using static 指令导入类的成员。 这允许我们使用静态成员,而无需使用类名限定它们。

thermopylae.txt
The Battle of Thermopylae was fought between an alliance of Greek city-states,
led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the
course of three days, during the second Persian invasion of Greece.

我们从这个文本文件中读取。

C# using 语句

必须在非托管资源(如文件、流或窗口句柄)上调用 Dispose 方法。 传统上,资源在 try/(catch)/finally 语句的 finally 块中释放。

当对象超出作用域时,using 语句会自动释放资源。 它还确保在发生异常时调用 Dispose

如果一个对象继承自 IDisposable 接口,则必须显式释放它。

Program.cs
using System.Text;

var path = "thermopylae.txt";

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);

string content = sr.ReadToEnd();

Console.WriteLine(content);

在此示例中,我们从文本文件中读取。

using var fs = new FileStream(path, FileMode.Open, FileAccess.Read);
using var sr = new StreamReader(fs, Encoding.UTF8);

FileStreamStreamReader 都继承自 IDisposable 接口。 使用 using 语句,我们可以确保当这两个对象超出作用域时(在我们的例子中,当它们到达文件末尾时)资源被正确释放。

旧的语法需要创建一个带有花括号的块。

Program.cs
using System.Text;

var path = "thermopylae.txt";

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{ 
    using (var sr = new StreamReader(fs, Encoding.UTF8))
    {
        string content = sr.ReadToEnd();
        Console.WriteLine(content);
    }
}

两个 using 语句都创建了它们自己的块。

可以将 using 语句分组在一起。

Program.cs
using System.Text;

var path = "thermopylae.txt";

using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read))
using (var sr = new StreamReader(fs, Encoding.UTF8))
{
    string content = sr.ReadToEnd();
    Console.WriteLine(content);
}

在该示例中,我们有一种替代语法; 我们创建一个包含两个 using 语句的块。

最旧的语法使用 try/finally 关键字。

Program.cs
using System.Text;

var path = "thermopylae.txt";

FileStream? fs = null;
StreamReader? sr = null;

try
{
    fs = new FileStream(path, FileMode.Open, FileAccess.Read);
    sr = new StreamReader(fs, Encoding.UTF8);

    string content = sr.ReadToEnd();
    Console.WriteLine(content);

} finally
{

    fs?.Close();
    sr?.Close();
}

finally 子句中,我们使用 Close 方法关闭资源。 请注意,Close 也会调用 Dispose

C# 引入了几种方便的方法来读取文本,例如 File.ReadAllLinesFile.ReadAllText。 这些方法会自动为我们调用 Dispose 方法。

Program.cs
using System.Text;

var path = "/home/janbodnar/Documents/thermopylae.txt";

string content = File.ReadAllText(path, Encoding.UTF8);
Console.WriteLine(content);

根据文档,ReadAllText 方法打开一个文本文件,将文件中的所有文本读取到字符串中,然后关闭该文件。

C# using 指令

using 指令允许我们使用特定命名空间的类型,这样我们就不必完全限定该命名空间中类型的使用。

Program.cs
int[] vals = [1, 4, -5, 3, -3, -1, 0, 2, 6, 7];
var positive = vals.Where(e => e > 0);

Console.WriteLine(string.Join(" ", positive));

我们在文件顶部使用两个 using 指令。 它们允许我们使用 Console.WriteLineWhere 方法。

但自从 C# 10 以来,这两个语句是多余的。 默认情况下,我们将它们包含在隐式全局 using 中。

C# using 指令用于别名

使用 using 指令,我们还可以创建别名。

Program.cs
using Terminal = System.Console;

Terminal.WriteLine("Hello there!");

在此示例中,我们为 System.Console 创建一个别名。

C# using static 指令

using static 允许我们访问类型的静态成员和嵌套类型,而无需使用类型名称限定访问。

Program.cs
using static System.Math;

Console.WriteLine(Sqrt(3*3 + 4*4));

在此示例中,我们将 Sqrt 方法导入到我们的命名空间中。 现在我们可以使用它,而无需 Math. 前缀。

来源

using 关键字 - 语言参考

在本文中,我们使用了 C# 中的 using 语句/指令。

作者

我叫 Jan Bodnar,是一位充满热情的程序员,拥有丰富的编程经验。 我从 2007 年开始撰写编程文章。 至今,我撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出所有 C# 教程