Using directive
Peponi │ 11/20/2024 │ 4m
C#
SyntaxKeywordnamespaceusing
Using directive
11/20/2024
4m
Peponi
C#
SyntaxKeywordnamespaceusing
1. Introduction
using
지시문은 네임스페이스에 정의된 형식을 정규화된 형식명 없이 사용 가능하게 해준다. using
지시문의 범위는 해당 namespace가 정의되는 파일이다. (using
지시문은 지정한 namespace 안의 nested namespace까지는 액세스 권한을 제공하지 않는다)
using
지시문은 다음 범위에 선언해야 한다.
- 소스 파일의 시작 부분
- namespace의 시작 부분
using
지시문에는 다음 한정자를 적용할 수 있다.
global
static
global static
1.1. Example
class Foo
{
void Bar() => System.Console.WriteLine("Hello world!");
}
using System;
class Foo
{
void Bar() => Console.WriteLine("Hello world!");
}
using
지시문을 사용하더라도, nested namespace의 경우 정규화된 형식명을 사용해야 접근이 가능하다.
namespace BaseNamespace
{
namespace NestedNamespace
{
public class Class
{
public void Method() => System.Console.WriteLine("Hello world!");
}
}
}
using BaseNamespace;
public class TestClass
{
private void Method()
{
// Class Class = new Class(); // CS0246
BaseNamespace.NestedNamespace.Class c = new BaseNamespace.NestedNamespace.Class();
c.Method();
}
}
2. Global using
C# 10부터는 using
지시문에 global
한정자를 추가하는 것이 가능하다. global
한정자는 프로젝트의 모든 파일에 해당 using
이 적용되는 것을 의미한다.
global using
지시문은 using
을 포함한 모든 항목 앞에 위치해야 한다.
global using System.Net;
using System.Threading;
using System.Threading;
global using System.Net;
TIP
global using
은 가능하면 하나의 파일에 추가하는 것이 좋다. 여러 곳에 분산되면 사용되는 using
을 관리하기 어려워질 수 있다.
3. Using static
using static
지시문은 정적 멤버 및 nested type에 액세스할 수 있는 기능을 제공한다.
namespace Foo
{
public class Bar
{
public static void Write() => System.Console.WriteLine("Hello world!");
public void Write2() => System.Console.WriteLine("Hello world2!");
}
}
using static Foo.Bar;
internal class Program
{
static void Main(string[] args)
{
Write();
// Write2(); // CS0103
}
}
4. Global using static
using
지시문은 global
및 static
한정자를 모두 적용할 수 있다. 이 때, 적용되는 특성은 global
+ static
이다.
namespace Foo
{
public class Bar
{
public static void Write() => System.Console.WriteLine("Hello world!");
public void Write2() => System.Console.WriteLine("Hello world2!");
}
}
global using static Foo.Bar;
namespace Other
{
public class Test
{
void Method() => Write();
}
}
5. Using alias
using
지시문은 별칭을 지정하는 것이 가능하다.
namespace Foo
{
public class Bar
{
public void Write() => System.Console.WriteLine("Hello world!");
}
}
using MyNamespace = Foo;
public class Test
{
void Method() => new MyNamespace.Bar().Write();
}
한편, using
별칭 지시문은 제네릭 형식을 가질 수 없다.
namespace Foo
{
public class Bar<T>
{
public void Write() => System.Console.WriteLine("Hello world!");
}
}
// using MyNamespace = Foo.Bar; // CS0305
using MyNamespace = Foo.Bar<int>;
public class Test
{
void Method() => new MyNamespace().Write();
}