Muokkaa

Namespaces and using directives

A C# file often starts with using directives followed by a namespace declaration:

using System;

namespace MyApp.Services;

class Greeter
{
    public void Greet(string name) => Console.WriteLine($"Hello, {name}!");
}

The example declares Greeter in the MyApp.Services namespace. Its complete name is MyApp.Services.Greeter. The using System; directive lets the code refer to types such as Console by their simple names.

Namespace declarations

A namespace is a named group of related types, such as classes, structs, and interfaces. Namespaces help you organize code and distinguish types with the same simple name. For example, your application can declare its own Timer without conflicting with a library's Timer when the types belong to different namespaces.

Use the namespace keyword to place your types in a namespace. The example uses a file-scoped namespace declaration: the semicolon after MyApp.Services makes the declaration apply to all types that follow in the file. Another form uses braces to enclose the types in a namespace.

Namespace names often match project folders, but C# doesn't require that convention. The declaration in your source code determines a type's namespace. Types declared outside a named namespace belong to the global namespace, the unnamed namespace that contains all named namespaces.

Using directives

A using directive lets you refer to types from a namespace without repeating the namespace name. For example, both of these statements call the same method:

System.Console.WriteLine("Hello!");
Console.WriteLine("Hello!"); // With using System;

System.Console is a fully qualified name: it includes both the namespace and the type name. A using directive changes how you write the name, not which type you use. It also doesn't add a library or package to your project. Your project must already have access to the types.

In a new console project, Console often works without an explicit using System;. The .NET SDK can generate common imports for you through the project's ImplicitUsings setting.

To declare namespaces, resolve name conflicts, and choose between file-local, global, and SDK-generated imports, continue with Organize types with namespaces.