What is a namespace?

A namespace helps organize a large group of related code. To use a namespace, call it with the using directive or include it before the class name.

Syntax

namespace NamespaceExample1Console
{
  // Empty Namespace
}

// In C# 10 file scoped namespaces were introduced. The namespace below does the same as the above namespace and will include everything in the file.
namespace NamespaceExample2Console;

Example: Using a namespace

The example below uses the System namespace. In the System namespace there’s a class called Console, and in the Console class there’s the WriteLine method.

Get hands-on with 1400+ tech skills courses.