Bite Bits/C#

C# 에서 new 한정자

상영 2017. 2. 7. 10:01

선언 한정자로 사용되는 new 키워드는 기본 클래스에서 상속된 멤버를 명시적으로 숨깁니다. 상속된 멤버를 숨기면 파생 버전의 멤버로 기본 클래스 버전의 멤버를 대신하게 됩니다. new 한정자를 사용하지 않고 멤버를 숨길 수도 있지만 컴파일러 경고가 발생합니다. new를 사용하여 멤버를 명시적으로 숨기면 이 경고가 발생하지 않습니다.

상속된 멤버를 숨기려면 동일한 멤버 이름을 사용하여 파생된 클래스에 해당 멤버를 선언한 다음 new 키워드를 사용하여 이를 한정합니다. 예:


    public class BaseC

    {

        public int x;

        public void Invoke() { }

    }

    public class DerivedC : BaseC

    {

        new public void Invoke() { }

    }


    public class BaseC 

    {

        public class NestedC 

        {

            public int x = 200;

            public int y;

        }

    }


    public class DerivedC : BaseC 

    {

        // Nested type hiding the base type members.

        new public class NestedC   

        {

            public int x = 100;

            public int y; 

            public int z;

        }


        static void Main() 

        {

            // Creating an object from the overlapping class:

            NestedC c1  = new NestedC();


            // Creating an object from the hidden class:

            BaseC.NestedC c2 = new BaseC.NestedC();


            Console.WriteLine(c1.x);

            Console.WriteLine(c2.x);   

        }

    }

    /*

    Output:

    100

    200

    */


출처 : https://msdn.microsoft.com/ko-kr/library/435f1dw2.aspx


C# 컴파일러 경로 : C:\Windows\Microsoft.NET  아래에 Framework 또는 Framework64 아래 각 버전별 폴더 아래에 있다.

파일명 csc.exe