What is a namespace in C#?

Answer

A namespace in C# provides a way to organize code into logical groups and prevent naming conflicts. It acts as a container for related classes, interfaces, enums, structs, and delegates. Declaration: namespace MyCompany.MyProduct.Services { public class UserService { } }. Usage: using MyCompany.MyProduct.Services; then var service = new UserService();. Without using directive, use the fully qualified name: var service = new MyCompany.MyProduct.Services.UserService();. File-scoped namespaces (C# 10+): namespace MyApp.Services; at the top of a file — applies to the whole file, reduces nesting. Global using directives (C# 10+): global using System; in one file applies to the entire project. Namespaces don't affect access modifiers — they're purely organizational. The .NET BCL namespaces: System, System.Collections.Generic, System.IO, System.Linq, etc.