Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Jun 16, 2025, 8:57 AM EDT
In C#, a namespace is a way to organize and group related classes, structs, interfaces, enums, and delegates. It helps avoid naming conflicts and makes code more readable and maintainable, especially in large projects.
Basic Syntax
namespace MyApplication { class MyClass { public void MyMethod() { Console.WriteLine("Hello from MyMethod!"); } } }Key Points:
Purpose: Prevents naming conflicts by encapsulating code in a named scope.
Usage: You can reference a namespace with the
usingkeyword.Nested Namespaces: You can nest namespaces to further organize code.
Using a Namespace
using MyApplication; class Program { static void Main() { MyClass obj = new MyClass(); obj.MyMethod(); } }Nested Namespace Example (C# 10+ allows this in one line)
namespace Company.Project.Module; class Feature { public void Run() { Console.WriteLine("Running feature..."); } }Common .NET Namespaces
System– Base namespace for core types (e.g.,System.String,System.Console)System.Collections.Generic– Collections likeList,DictionarySystem.IO– File and stream input/outputSystem.Threading.Tasks– For asynchronous programmingSam HobbsRank #107 · 20.5K reputationPosted Jun 16, 2025, 1:59 PM EDT
There is a one-for-one correspondence of assembly (DLL) and namespace. In other words, a namespace is a .Net API for a .Net DLL. For example the String Class is in the
Systemnamespace and theSystem.Runtime.dllassembly except for .Net Standard it is in thenetstandard.dllassembly.Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Jun 17, 2025, 12:19 AM EDT
A namespace in C# is used to organize code and avoid naming conflicts between classes, especially in large projects. Think of it as a container that groups related classes, interfaces, enums, etc.
// Simple Example Code namespace MyApp.Utilities { class Logger { public void Log(string message) { Console.WriteLine("Log: " + message); } } } // Using the Namespace using MyApp.Utilities; class Program { static void Main() { Logger logger = new Logger(); logger.Log("Namespace example running."); } }Good Luck!