0Pricing
C# Academy · Lesson

Namespaces, using directives, global usings (note)

Organize types with namespaces, import namespaces with using (and aliases), avoid name collisions, and note that global using is newer than C# 6.

Namespaces & using: overview

Goal: Organize and import code.

  • namespace groups related types
  • using imports namespaces
  • Aliases resolve name clashes
  • global using is newer than C# 6 (info only)

Declare namespace & type

Define a namespace to group types; you can always use the fully qualified name to access a type.

using System;

namespace MyApp.Models
{
  public class User
  {
    public string Name { get; private set; }
    public User(string name) { Name = name; }
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    // Fully-qualified name without using
    MyApp.Models.User u = new MyApp.Models.User("Ada");
    Console.WriteLine(u.Name);
  }
}

All lessons in this course

  1. public/internal/protected/private (with note on file)
  2. Namespaces, using directives, global usings (note)
  3. Partial types & files
← Back to C# Academy