Jump to content
Wikibooks The Free Textbook Project

C# Programming/Keywords/using

From Wikibooks, open books for an open world
The latest reviewed version was checked on 16 April 2020. There are template/file changes awaiting review.


The using keyword has two completely unrelated meanings in C#, depending on if it is used as a directive or a statement.

The directive

[edit | edit source ]

using as a directive resolves unqualified type references so that a developer doesn't have to specify the complete namespace.

Example:

usingSystem;

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

using can also provide a namespace alias for referencing types.

Example:

usingutils=Company.Application.Utilities;

The statement

[edit | edit source ]

using 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.StreamReaderreader=newStreamReader("readme.txt"))
{
// read from the file
}

// The file readme.txt has now been closed automatically.
using(FontheaderFont=newFont("Arial",12.0f),
textFont=newFont("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)

AltStyle によって変換されたページ (->オリジナル) /