Namespace alias operator
Peponi │ 12/26/2024 │ 2m
C#
SyntaxOperator::
Namespace alias operator
12/26/2024
2m
Peponi
C#
SyntaxOperator::
1. Introduction
네임스페이스 별칭 한정자 (::
) 는 별칭이 있는 네임스페이스의 구성원에 접근할 수 있게 하며 다음 경우에 사용할 수 있다.
- Using alias
- Extern alias
- Global alias : 전역 네임스페이스 별칭
2. Example
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!
*/
namespace ClassLibrary1
{
public static class Class
{
public static void Write(string message) => Console.WriteLine(message);
}
}
<Reference Include="ClassLibrary1" Aliases="Import">
<HintPath>..\ClassLibrary1.dll</HintPath>
</Reference>
extern alias Import;
namespace ConsoleApp
{
internal class Program
{
static void Main(string[] args)
{
Import::ClassLibrary1.Class.Write("Hello, World!");
}
}
}
/* output:
Hello, World!
*/
global::System.Console.WriteLine("Hello, World!");
/* output:
Hello, World!
*/