Skip to content

ES CSharp Keyword alias

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: alias

Manual C# · Guía Práctica

Un alias de ensamblado externo agrega un espacio de nombres raíz independiente. Le permite distinguir entre nombres de tipos completos idénticos de diferentes ensamblajes. Se especifican alias para las referencias en el proyecto; en el código, el alias se declara mediante un alias externo y se usa con el operador ::.

Ejemplo sencillo (Example.cs)

Guarde juntos todos los archivos indicados y ejecute Example.csproj. El archivo Example.cs por sí solo no configura la referencia con el alias Tools.

Example.cs:

// PROJECT-EXAMPLE
extern alias Tools;
System.Console.WriteLine(Tools::Demo.Number.Value);

Dependency.cs:

// PROJECT-EXAMPLE
namespace Demo; public static class Number { public static int Value => 7; }

Dependency.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Dependency.cs" /></ItemGroup></Project>

Example.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net10.0</TargetFramework><EnableDefaultCompileItems>false</EnableDefaultCompileItems></PropertyGroup><ItemGroup><Compile Include="Example.cs" /><ProjectReference Include="Dependency.csproj"><Aliases>Tools</Aliases></ProjectReference></ItemGroup></Project>
dotnet run --project Example.csproj

Resultado:

7

Sintaxis

extern alias Legacy;
using OldLabel = Legacy::Shared.Label;

Acceder a una biblioteca local a través de un alias

Archivo Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    
  </PropertyGroup>
  <ItemGroup>
    <Compile Remove="Dependency/**/*.cs" />
    <ProjectReference Include="Dependency/Dependency.csproj"><Aliases>Legacy</Aliases></ProjectReference>
  </ItemGroup>
</Project>

Archivo Program.cs:

extern alias Legacy;
using System;

internal static class Program
{
    public static void Main()
    {
        var label = new Legacy::Shared.Label("Local library");
        Console.WriteLine(label.Text);
    }
}

Archivo Dependency/Dependency.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

Archivo Dependency/Label.cs:

namespace Shared;
public sealed class Label(string text)
{
    public string Text { get; } = text;
}

Resultado:

Local library

Dos bibliotecas con el mismo nombre de tipo completo

Archivo Example.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net10.0</TargetFramework>
    <LangVersion>14.0</LangVersion>
    <Nullable>enable</Nullable>
    
  </PropertyGroup>
  <ItemGroup>
    <Compile Remove="LegacyDependency/**/*.cs;ModernDependency/**/*.cs" />
    <ProjectReference Include="LegacyDependency/LegacyDependency.csproj"><Aliases>OldApi</Aliases></ProjectReference>
    <ProjectReference Include="ModernDependency/ModernDependency.csproj"><Aliases>NewApi</Aliases></ProjectReference>
  </ItemGroup>
</Project>

Archivo Program.cs:

extern alias OldApi;
extern alias NewApi;
using System;
using OldCounter = OldApi::Shared.Counter;
using NewCounter = NewApi::Shared.Counter;

internal static class Program
{
    public static void Main()
    {
        Console.WriteLine($"Old: {OldCounter.Scale(5)}");
        Console.WriteLine($"New: {NewCounter.Scale(5)}");
    }
}

Archivo LegacyDependency/LegacyDependency.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

Archivo LegacyDependency/Counter.cs:

namespace Shared;
public static class Counter { public static int Scale(int value) => value * 2; }

Archivo ModernDependency/ModernDependency.csproj:

<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net10.0</TargetFramework></PropertyGroup></Project>

Archivo ModernDependency/Counter.cs:

namespace Shared;
public static class Counter { public static int Scale(int value) => value * 3; }

Resultado:

Old: 10
New: 15

Fuente oficial: Microsoft Aprender.

Clone this wiki locally