■ 분해자(Deconstructor)를 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
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 73 74 75 |
using System; User user = new User { FirstName = "Joe", LastName = "Bloggs", Age = 23, MailAddress = "joe.bloggs@example.com" }; (string firstName, string lastName) = user; Console.WriteLine($"The user's name is {firstName} {lastName}"); /// <summary> /// 사용자 /// </summary> public class User { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion #region 나이 - Age /// <summary> /// 나이 /// </summary> public int Age { get; set; } #endregion #region 메일 주소 - MailAddress /// <summary> /// 메일 주소 /// </summary> public string MailAddress { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Deconstructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 분해자 - Deconstruct(firstName, lastName) /// <summary> /// 분해자 /// </summary> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public void Deconstruct(out string firstName, out string lastName) { firstName = FirstName; lastName = LastName; } #endregion } |
※ 필요시 누겟 설치 : System.ValueTuple