Skip to content

EN CSharp Namespaces Extensions

Codex edited this page Oct 11, 2026 · 2 revisions

Українська · Русский · English · Deutsch · Français · Italiano · Español · 繁體中文 · 日本語 · 한국어

Namespaces, using and extension methods

Manual C# · Examples UO

namespace groups types and helps avoid name collisions. using imports names; an alias provides another name for a namespace or type, and using static imports static members. global using applies throughout a regular project.

Simple example (Example.cs)

Save this example separately as Example.cs. Run it independently of the other examples on this page.

Example.cs:

// PROJECT-EXAMPLE
using System;
Console.WriteLine("ore".Twice());
static class TextTools { public static string Twice(this string value) => value + value; }

Result:

oreore
// PROJECT-EXAMPLE
using System;
using Number = System.Int32;
using static System.Math;

namespace UserTools;

public static class Calculation
{
    public static Number Clamp(Number value) => Max(0, Min(100, value));
}

The extension method is declared in a suitable static type of the regular .cs-project. It allows a static method to be called as a member of an object without changing the original type declaration. The extension code can change the state of the passed object if its API allows such a change. In .csx you can use already connected extensions, for example LINQ; Design namespace declarations and library extensions in regular sources.

// PROJECT-EXAMPLE
using System;

namespace UserTools;

public static class NumberExtensions
{
    public static bool IsPositive(this int value) => value > 0;
}

public static class Example
{
    public static bool Run() => (3).IsPositive();
}

Official documentation: Namespaces, using and extension methods - Microsoft Learn.

Clone this wiki locally