■ switch문에서 튜플 패턴(Tuple Pattern)을 사용하는 방법을 보여준다.
▶ School.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 |
namespace TestProject { /// <summary> /// 학교 /// </summary> public class School { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get; set; } #endregion #region 명칭 - Name /// <summary> /// 명칭 /// </summary> public string Name { get; set; } #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 46 47 |
using System; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string description = GetDescription(new School { ID = 3, Name = "보성고" }); Console.WriteLine(description); } #endregion #region 설명 구하기 - GetDescription(school) /// <summary> /// 설명 구하기 /// </summary> /// <param name="school">학교</param> /// <returns>설명</returns> private static string GetDescription(School school) => (school.ID, school.Name) switch { (1, "동북고") => "남자 학교", (2, "영파고") => "여자 학교", (3, "보성고") => "남자 학교", (_, _ ) => "해당 무" }; #endregion } } |