Namespaces

View saved

Declare a namespace

Namespaces group related types and avoid name clashes.

namespace FreeCourses.Lessons;

public class Lesson
{
    public required string Title { get; set; }
}

Import with using

Bring a namespace into scope at the top of a file.

using System.Text;

var sb = new StringBuilder();
sb.Append("C#");
Console.WriteLine(sb);

File-scoped namespaces

A single namespace line keeps indentation shallow in modern C#.

Organization tips

  • Match folder structure to namespaces as projects grow
  • Avoid overly deep namespace trees for small apps
  • Global usings in SDK-style projects reduce boilerplate
  • Fully qualify rare names when imports collide

Comments

One comment per signed-in account. Comments are saved with this page’s URL.