9. Namespaces & Assemblies


Part 1: Namespaces

1. What are Namespaces?

Purpose:

  • Organize code into logical groups

  • Avoid naming conflicts

  • Provide clear hierarchy and structure

Namespace vs Assembly:

  • Namespace - Logical organization (in code)

  • Assembly - Physical packaging (.dll or .exe file)

  • One assembly can contain multiple namespaces

  • One namespace can span multiple assemblies

// Different classes with same name in different namespaces
namespace MyCompany.UI
{
    public class Button { }  // UI button
}

namespace MyCompany.Hardware
{
    public class Button { }  // Physical button sensor
}

2. Declaring Namespaces

Block-scoped (Traditional)

File-scoped (C# 10.0+) - Modern ✅

Nested Namespaces


3. Using Directives

Basic using Statement

using static (C# 6.0+)

using Alias

Alias any Type (C# 12.0+)

Global using (C# 10.0+)


4. Implicit Usings (.NET 6.0+)

What are Implicit Usings? Common namespaces automatically included based on project type.

Enable in .csproj

Default Implicit Usings by Project Type

Console App:

Web API / ASP.NET Core:

Add Custom Implicit Usings

Remove Implicit Usings


5. Namespace Best Practices

Naming Conventions:

Match Folder Structure:

Avoid System.*

One Namespace Per File (guideline)

Don't:

  • Use too many nested levels (3-4 max)

  • Create namespace for single class

  • Mix unrelated types in one namespace


Part 2: Essential Namespaces Reference

Core Namespaces ⭐⭐⭐ (Must Know)

System

Purpose: Fundamental types and base classes

Key Types:

System.Collections.Generic

Purpose: Generic collection types

Key Types:

System.Linq

Purpose: LINQ query operators

Key Types:

System.Threading.Tasks

Purpose: Async/await and task-based programming

Key Types:

System.IO

Purpose: File and stream I/O

Key Types:

System.Text

Purpose: Text encoding and manipulation

Key Types:


Common Namespaces ⭐⭐ (Frequently Used)

System.Text.RegularExpressions

System.Threading

System.Diagnostics

System.Net.Http

System.Text.Json

System.Collections.Concurrent


Specialized Namespaces ⭐ (As Needed)

System.Reflection

System.Numerics

System.Security.Cryptography


Common Using Combinations by Scenario

Console Application

File I/O

Async Programming

Data Processing

Web API Client

Database Operations


Part 3: Assemblies

10. What are Assemblies?

Definition: Compiled code library (.dll or .exe)

Assembly Components:


11. Assembly Types

Private Assemblies

  • Application-specific

  • Located in application folder

  • No versioning conflicts

  • Most common type

Shared Assemblies

  • Stored in GAC (Global Assembly Cache)

  • Requires strong name

  • Shared across applications

  • Rarely used in modern .NET

Static Assemblies

  • Stored on disk (.dll or .exe files)

  • Standard assemblies

Dynamic Assemblies

  • Created in memory at runtime

  • Using Reflection.Emit

  • Advanced scenarios only


12. DLL vs EXE

EXE (Executable)

DLL vs EXE Comparison

Feature
DLL
EXE

Execution

Cannot run independently

Can run independently

Entry Point

No Main() method

Has Main() method

Purpose

Library/component

Application

Extension

.dll

.exe

Sharing

Can be shared by multiple apps

Standalone

Referenced by

Other projects

N/A (top-level)


13. Strong Naming

What is Strong Naming? Signing assembly with public/private key pair for uniqueness and security.

Components:

  • Simple name (assembly name: "MyLibrary")

  • Version number (1.0.0.0)

  • Culture information (en-US, neutral, etc.)

  • Public key token (from key pair)

Creating Strong-Named Assembly:

  1. Generate key pair:

  1. Add to .csproj:

Benefits:

  • ✅ Uniqueness guarantee (no name collisions)

  • ✅ Version enforcement

  • ✅ Tamper protection

  • ✅ Can be placed in GAC

  • ✅ Trust level for security

When to Use:

  • Shared libraries (GAC deployment)

  • Versioning critical

  • Security requirements

  • Large organizations


14. Assembly Versioning

Version Format

Version Types

Semantic Versioning (SemVer)

SemVer Rules:

  • Increment MAJOR: Breaking changes

  • Increment MINOR: New features, backward compatible

  • Increment PATCH: Bug fixes only


15. Assembly Loading

Loading Methods

Probing Paths

CLR searches for assemblies in:

  1. Application base directory

  2. Private bin path (if specified)

  3. GAC (for strong-named assemblies)


16. Assembly Scope & Visibility

internal Access Modifier

InternalsVisibleTo Attribute

Common Use Case: Unit Testing


Part 4: Project Structure

17. .NET Project File (.csproj)

SDK-Style Project (Modern)


18. Solution Files (.sln)

What is a Solution? Container for one or more projects.

Benefits:

  • Build multiple projects together

  • Manage dependencies

  • Share configurations

  • Organize related projects

Solution Structure:

Build Configurations:

  • Debug (development)

  • Release (production)

  • Custom configurations


19. NuGet Packages

Package Sources:

  • nuget.org (official public repository)

  • Private feeds (Azure Artifacts, MyGet)

  • Local folders

Package Versioning:

Common NuGet Packages:


20. Target Frameworks

Available Frameworks

.NET Framework (Windows only) - Legacy

.NET Core (cross-platform) - Superseded

.NET 5+ (unified platform) - Modern ✅

.NET Standard (API specification) - For libraries

Multi-Targeting

Framework Compatibility Matrix

Target
Can Reference

.NET 8.0

.NET 8.0, .NET Standard 2.1, .NET Standard 2.0

.NET 6.0

.NET 6.0, .NET Standard 2.1, .NET Standard 2.0

.NET Framework 4.8

.NET Framework 4.8, .NET Standard 2.0

.NET Standard 2.1

.NET Standard 2.1, .NET Standard 2.0, .NET Standard 1.x

.NET Standard 2.0

.NET Standard 2.0, .NET Standard 1.x


21. Project Organization Best Practices

Namespace = Folder Path

Best Practices

Do:

  • One type per file (easier navigation, better source control)

  • Match namespace to folder structure

  • Use partial classes for generated code

  • Separate concerns (UI, Business Logic, Data Access)

  • Group related projects in solution folders

Don't:

  • Mix unrelated code in same project

  • Create circular dependencies

  • Put everything in root folder

  • Use "Utilities" or "Helpers" folders (be specific)

Separation of Concerns


Quick Reference Summary

Namespaces

  • Purpose: Organize code, avoid conflicts

  • Modern syntax: File-scoped (C# 10+)

  • Global usings: Available across all files (C# 10+)

  • Implicit usings: Auto-included by project type (.NET 6+)

Essential Namespaces

  • System - Fundamentals (Object, String, Console, Math, DateTime)

  • System.Collections.Generic - Collections (List, Dictionary, HashSet)

  • System.Linq - LINQ operators

  • System.Threading.Tasks - Async/await (Task, Task<T>)

  • System.IO - File operations

  • System.Text - StringBuilder, Encoding

Assemblies

  • Definition: Compiled code (.dll or .exe)

  • DLL: Library (no Main method)

  • EXE: Application (has Main method)

  • Strong naming: Sign with key for uniqueness/security

  • Versioning: Major.Minor.Build.Revision

Project Organization

  • .csproj: Project file (SDK-style for modern .NET)

  • .sln: Solution file (container for projects)

  • NuGet: Package management

  • Target frameworks: net8.0, net6.0, net48, netstandard2.0

  • Folder structure: src/, tests/, docs/, tools/


Guide Complete! This reference will help you organize your C# projects professionally! 📘

Last updated