Favicon

Namespace alias operator

Peponi12/26/20242m

C#
SyntaxOperator::

1. Introduction

네임스페이스 별칭 한정자 (::) 는 별칭이 있는 네임스페이스의 구성원에 접근할 수 있게 하며 다음 경우에 사용할 수 있다.

2. Example

Using alias
namespace MyConsole
{
    public static class MyWriter
    {
        public static void Write(string message) => Console.WriteLine(message);
    }
}
 
namespace ConsoleApp
{
    using Writer = MyConsole;
 
    internal class Program
    {
        static void Main(string[] args)
        {
            Writer::MyWriter.Write("Hello, World!");
        }
    }
}
 
/* output:
Hello, World!
*/
Extern alias - Assembly
namespace ClassLibrary1
{
    public static class Class
    {
        public static void Write(string message) => Console.WriteLine(message);
    }
}
Extern alias - *.csproj
<Reference Include="ClassLibrary1" Aliases="Import">
  <HintPath>..\ClassLibrary1.dll</HintPath>
</Reference>
Extern alias - Program.cs
extern alias Import;
 
namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Import::ClassLibrary1.Class.Write("Hello, World!");
        }
    }
}
 
/* output:
Hello, World!
*/
Global alias
global::System.Console.WriteLine("Hello, World!");
 
/* output:
Hello, World!
*/

3. 참조 자료