LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 4010 min read

Namespaces

Learn how namespaces organize code and prevent naming collisions in larger C# projects.

What is a Namespace?

A namespace groups related classes together, and helps avoid naming collisions when two different libraries happen to define a class with the same name.

Declaring Namespaces

namespace PrograMinds.Models
{
public class Student
{
public string Name;
}
}
namespace PrograMinds.Billing
{
public class Student // a totally different class, no conflict
{
public decimal Balance;
}
}
File-Scoped Namespaces

Since C# 10, you can write `namespace PrograMinds.Models;` at the top of a file (no braces) to apply it to the whole file — slightly less indentation for the common case of one namespace per file.

The using Directive

`using` brings a namespace's types into scope so you don't have to write the full path every time.

using PrograMinds.Models;
var student = new Student(); // no need for PrograMinds.Models.Student

FAQs

You can use the fully qualified name (like `PrograMinds.Models.Student`) or an alias with `using Student = PrograMinds.Models.Student;`.

Summary

  • Namespaces organize code and prevent naming collisions.
  • File-scoped namespaces (C# 10+) reduce indentation.
  • `using` brings a namespace's types into scope without fully qualifying them.
Next Lesson →

Nullable Types