> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# C# Extern Alias

An `extern alias` is a C# language feature that introduces a distinct root-level identifier for an external assembly. It allows the compiler to disambiguate between types that share the exact same fully qualified name (identical namespaces and type names) but reside in different referenced assemblies. By mapping an assembly to an external alias, the assembly's global namespace is nested under the specified alias rather than the default `global::` namespace.

## Syntax and Declaration

An `extern alias` directive can be declared at the root of a compilation unit (file) or within a namespace body. In either context, the directive must precede all `using` directives and member declarations within that specific scope.

To access types within the aliased assembly, you use the namespace alias qualifier operator (`::`).

```csharp theme={"dark"}
// File-scoped extern alias declaration
extern alias LibraryV1;

using System;
using LegacyGrid = LibraryV1::Controls.DataGrid;

namespace Application
{
    // Namespace-scoped extern alias declaration
    extern alias LibraryV2;
    
    using ModernGrid = LibraryV2::Controls.DataGrid;

    public class GridManager
    {
        public void InitializeGrids()
        {
            // Instantiation via the using alias
            LegacyGrid oldGrid = new LegacyGrid();
            
            // Inline instantiation using the extern alias directly
            LibraryV2::Controls.DataGrid newGrid = new LibraryV2::Controls.DataGrid();
        }
    }
}
```

## Compiler Configuration

The `extern alias` directive in code requires an explicit mapping between the alias identifier used in the source code and the physical assembly file during compilation.

### MSBuild (.csproj) Integration

In modern .NET project files, the mapping is defined using the `<Aliases>` metadata tag within an assembly `<Reference>` or `<PackageReference>` node.

```xml theme={"dark"}
<ItemGroup>
  <Reference Include="Controls.Library.V1">
    <HintPath>..\lib\v1\Controls.Library.dll</HintPath>
    <Aliases>LibraryV1</Aliases>
  </Reference>
  
  <Reference Include="Controls.Library.V2">
    <HintPath>..\lib\v2\Controls.Library.dll</HintPath>
    <Aliases>LibraryV2</Aliases>
  </Reference>
</ItemGroup>
```

### Command-Line Compiler (csc.exe)

If invoking the C# compiler directly, the alias is passed via the `/reference` (or `/r`) flag, appending the alias name and an equals sign before the assembly path.

```bash theme={"dark"}
csc /reference:LibraryV1=v1\Controls.Library.dll /reference:LibraryV2=v2\Controls.Library.dll Program.cs
```

## Technical Characteristics

* **The `global` Context:** By default, all referenced assemblies are implicitly merged into the `global` alias. When you assign a custom alias to an assembly, it is removed from the `global` namespace unless you explicitly declare multiple aliases for it (e.g., `<Aliases>global,LibraryV1</Aliases>`).
* **Scope:** The scope of an `extern alias` directive depends on its declaration location. If declared at the root of a compilation unit, it is file-scoped. If declared within a namespace block, it is namespace-scoped and applies only to declarations within that specific namespace body.
* **Resolution Hierarchy:** The compiler resolves the identifier on the left side of the `::` operator by first checking for `extern alias` declarations, ensuring that the alias root takes precedence over identically named namespaces within the current project.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
