跳转至内容

C# 编程/关键字/using

来自维基教科书,开放的书籍,造福全世界


在 C# 中,using 关键字有两个完全不相关的含义,取决于它是用作指令还是语句。

using 作为指令解析无限定类型引用,这样开发者就不必指定完整的命名空间。

例子

using System;
 
// A developer can now type ''Console.WriteLine();'' rather than ''System.Console.WriteLine()''.

using 还可以为引用类型提供命名空间别名

例子

using utils = Company.Application.Utilities;

using 作为语句会自动调用指定对象的 dispose。 该对象必须实现 IDisposable 接口。只要它们是相同类型,就可以在一个语句中使用多个对象。

例子

using (System.IO.StreamReader reader = new StreamReader("readme.txt"))
{
    // read from the file
}
 
// The file readme.txt has now been closed automatically.

using (Font headerFont = new Font("Arial", 12.0f),
            textFont = new Font("Times New Roman", 10.0f))
{
    // Use headerFont and textFont.
}

// Both font objects are closed now.



C# 关键字
abstract as base bool break
byte case catch char checked
class const continue decimal default
delegate do double else enum
event explicit extern false finally
fixed float for foreach goto
if implicit in int interface
internal is lock long namespace
new null object operator out
override params private protected public
readonly ref return sbyte sealed
short sizeof stackalloc static string
struct switch this throw true
try typeof uint ulong unchecked
unsafe ushort using var virtual
void volatile while
C# 特殊标识符(上下文关键字)
add alias async await dynamic
get global nameof partial remove
set value when where yield
上下文关键字(用于查询)
ascending by descending equals from
group in into join let
on orderby select where
华夏公益教科书