Math 클래스와 using static 구문 함께 사용하기

    Console 클래스와 마찬가지로 Math 클래스도 using static 구문을 사용하여 줄여서 표현할 수 있습니다. 이번에는 using static 지시문을 사용해 보겠습니다. 다음 내용을 입력한 후 실행해 보세요.

    Math 클래스와 using static 구문 함께 사용: UsingStaticClassesDemo.cs

    using System;
    using static System.Console;
    using static System.Math;
    
    class UsingStaticClassesDemo
    {
        static void Main()
        {
            //① 기본 사용 방식
            WriteLine(Math.Pow(2, 10));
    
            //② using static 지시문으로 줄여서 표현
            WriteLine(Pow(2, 10));
            WriteLine(Max(3, 5));
        }
    }

    실행 결과

    1024
    1024
    5
    계속하려면 아무 키나 누르십시오 . . .

    ConsoleMath 클래스를 여러 번 사용한다면 using static 구문으로 생략하여 코드를 간결하게 유지할 수 있습니다.

    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.