■ 구조체 읽기 전용 멤버를 사용하는 방법을 보여준다.
▶ Triangle.cs
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 60 61 62 63 64 65 66 67 68 69 70 71 72 |
namespace TestProject { /// <summary> /// 삼각형 /// </summary> public struct Triangle { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 길이 1 /// </summary> public int Length1; /// <summary> /// 길이 2 /// </summary> public int Length2; /// <summary> /// 길이 3 /// </summary> public int Length3; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 둘레 - Perimeter /// <summary> /// 둘레 /// </summary> public readonly int Perimeter => Length1 + Length2 + Length3; #endregion #region 등변 여부 - IsEquilateral /// <summary> /// 등변 여부 /// </summary> public readonly bool IsEquilateral => Length1 == Length2 && Length2 == Length3; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Triangle(length1, length2, length3) /// <summary> /// 생성자 /// </summary> /// <param name="length1">길이 1</param> /// <param name="length2">길이 2</param> /// <param name="length3">길이 3</param> public Triangle(int length1, int length2, int length3) { Length1 = length1; Length2 = length2; Length3 = length3; } #endregion } } |
▶ Program.cs
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 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { Triangle triangle = new Triangle(3, 4, 5); Print(triangle); } #endregion #region 출력하기 - Print(triangle) /// <summary> /// 출력하기 /// </summary> /// <param name="triangle">삼각형</param> private static void Print(in Triangle triangle) { int perimeter = triangle.Perimeter; bool isEquilateral = triangle.IsEquilateral; Console.WriteLine($"{perimeter}, {isEquilateral}"); } #endregion } } |