. 연산자(C# 참조)

도트 연산자(.)는 멤버 액세스에 사용됩니다.도트 연산자는 형식 또는 네임스페이스의 멤버를 지정합니다.예를 들어, 도트 연산자는 .NET Framework 클래스 라이브러리 내의 특정 메서드에 액세스하는 데 사용됩니다.

// The class Console in namespace System:
System.Console.WriteLine("hello");

예를 들어, 다음 클래스를 확인해 보십시오.

class Simple
{
    public int a;
    public void b()
    {
    }
}
Simple s = new Simple();

변수 s에는 a 및 b의 두 멤버가 있습니다. 이들 멤버에 액세스하기 위해 다음과 같이 도트 연산자를 사용합니다.

s.a = 6;   // assign to field a;
s.b();     // invoke member function b;

또한 해당 멤버가 속한 네임스페이스나 인터페이스 등을 지정하는 정규화된 이름을 형성하기 위해서도 도트 연산자를 사용합니다.

// The class Console in namespace System:
System.Console.WriteLine("hello");

다음과 같이 using 지시문을 사용하면 일부 이름 한정이 선택적인 요소가 됩니다.


namespace ExampleNS
{
    using System;
    class C
    {
        void M()
        {
            System.Console.WriteLine("hello");
            Console.WriteLine("hello");   // Same as previous line.
        }
    }
}

하지만 다음과 같이 식별자가 모호할 경우에는 한정이 필요합니다.

namespace Example2
{
    class Console
    {
        public static void WriteLine(string s){}
    }
}
namespace Example1
{
    using System;
    using Example2;
    class C
    {
        void M()
        {                
            // Console.WriteLine("hello");   // Compiler error. Ambiguous reference.
            System.Console.WriteLine("hello"); //OK
            Example2.Console.WriteLine("hello"); //OK
        }
    }
}

C# 언어 사양

자세한 내용은 C# 언어 사양을 참조하십시오. 이 언어 사양은 C# 구문 및 사용법에 대한 신뢰할 수 있는 소스입니다.

참고 항목

참조

C# 연산자

개념

C# 프로그래밍 가이드

기타 리소스

C# 참조