프로그래밍(Programming)/C#
using 정적 지시문, using static System.Console;
3DMP
2018. 8. 6. 16:02
using static System.Console;
C# 6.0 이후 버전 이후부터 위 구문을 사용하여 WriteLine 명령어를 간단하게 사용 할 수 잇다
다음 예제에서는 형식 이름을 지정할 필요 없이 using static
지시문을 사용하여 Console, Math 및 String 클래스의 정적 멤버를 사용 가능하게 합니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | using System; using static System.Console; using static System.Math; using static System.String; class Program { static void Main() { Write("Enter a circle's radius: "); var input = ReadLine(); if (!IsNullOrEmpty(input) && double.TryParse(input, out var radius)) { var c = new Circle(radius); string s = "\nInformation about the circle:\n"; s = s + Format(" Radius: {0:N2}\n", c.Radius); s = s + Format(" Diameter: {0:N2}\n", c.Diameter); s = s + Format(" Circumference: {0:N2}\n", c.Circumference); s = s + Format(" Area: {0:N2}\n", c.Area); WriteLine(s); } else { WriteLine("Invalid input..."); } } } public class Circle { public Circle(double radius) { Radius = radius; } public double Radius { get; set; } public double Diameter { get { return 2 * Radius; } } public double Circumference { get { return 2 * Radius * PI; } } public double Area { get { return PI * Pow(Radius, 2); } } } // The example displays the following output: // Enter a circle's radius: 12.45 // // Information about the circle: // Radius: 12.45 // Diameter: 24.90 // Circumference: 78.23 // Area: 486.95 |
ref : https://docs.microsoft.com/ko-kr/dotnet/csharp/language-reference/keywords/using-static
반응형