Skip to content

ZH TW CSharp Keyword alias

Codex edited this page Oct 11, 2026 · 2 revisions

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

C#: alias

使用手冊C# · 實用指南

外部程式集別名添加單獨的根命名空間。它允許您區分來自不同程序集的相同完全限定類型名稱。指定別名以供項目中引用;在程式碼中,別名是透過 extern alias 聲明的,並與 :: 運算子一起使用。

簡單範例 (Example.cs)

請將所有範例檔案儲存在同一目錄,並執行 Example.csproj。單獨的 Example.cs 不會設定 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

結果:

7

句法

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

透過別名存取本地庫

文件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>

文件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);
    }
}

文件Dependency/Dependency.csproj:

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

文件Dependency/Label.cs:

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

結果:

Local library

兩個函式庫具有相同的完全限定型別名稱

文件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>

文件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)}");
    }
}

文件LegacyDependency/LegacyDependency.csproj:

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

文件LegacyDependency/Counter.cs:

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

文件ModernDependency/ModernDependency.csproj:

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

文件ModernDependency/Counter.cs:

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

結果:

Old: 10
New: 15

官方來源:Microsoft學習。

Clone this wiki locally