C# Programming/Keywords/using
The using
keyword has two completely unrelated meanings in C#, depending on if it is used as a directive or a statement.
The directive
editusing
as a directive resolves unqualified type references so that a developer doesn't have to specify the complete namespace.
Example:
using System;
// A developer can now type ''Console.WriteLine();'' rather than ''System.Console.WriteLine()''.
using
can also provide a namespace alias for referencing types.
Example:
using utils = Company.Application.Utilities;
The statement
editusing
as a statement automatically calls the dispose
on the specified object. The object must implement the IDisposable
interface. It is possible to use several objects in one statement as long as they are of the same type.
Example:
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# Keywords | |||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Special C# Identifiers (Contextual Keywords) | |||||||||||||||
| |||||||||||||||
Contextual Keywords (Used in Queries) | |||||||||||||||
|