■ ref 키워드를 사용해 참조 로컬을 만드는 방법을 보여준다. ▶ ArtistStore.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
|
namespace TestProject; /// <summary> /// 예술가 저장소 /// </summary> public class ArtistStore { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 예술가 배열 /// </summary> private readonly string[] artistArray = new[] { "Amenra", "The Shadow Ring", "Hiroshi Yoshimura" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 모든 예술가 목록 - AllAritstList /// <summary> /// 모든 예술가 목록 /// </summary> public string AllAritstList => string.Join(", ", artistArray); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 첫번째 예술가 구하기 - GetFirstArtist() /// <summary> /// 첫번째 예술가 구하기 /// </summary> /// <returns>첫번째 예술가</returns> public string GetFirstArtist() { return artistArray[1]; } #endregion #region 참조로 첫번째 예술가 구하기 - GetArtistByReference() /// <summary> /// 참조로 첫번째 예술가 구하기 /// </summary> /// <returns>첫번째 예술가</returns> public ref string GetArtistByReference() { return ref artistArray[1]; } #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 48 49 50 51 52 53
|
namespace TestProject; /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { ArtistStore store = new ArtistStore(); Console.WriteLine(store.AllAritstList); Console.WriteLine(); string artist = store.GetFirstArtist(); artist = "Henry Cow"; Console.WriteLine(store.AllAritstList); Console.WriteLine(); artist = store.GetArtistByReference(); artist = "Frank Zappa"; Console.WriteLine(store.AllAritstList); Console.WriteLine(); ref string artistReference = ref store.GetArtistByReference(); artistReference = "Valentyn Sylvestrov"; Console.WriteLine(store.AllAritstList); Console.WriteLine(); } #endregion } |
TestProject.zip
■ switch문에서 타입 패턴을 사용하는 방법을 보여준다. ▶ 예제 코드 (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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
using System; using System.Collections.Generic; #region 도형 출력하기 - PrintShape(shapeList) /// <summary> /// 도형 출력하기 /// </summary> /// <param name="shapeList">도형 리스트</param> private void PrintShape(List<Shape> shapeList) { foreach(Shape shape in shapeList) { switch(shape) { case null : Console.WriteLine("NULL"); break; case Circle circle : Console.WriteLine($"RADIUS={circle.Radius}"); break; case Rectangle rectangle when rectangle.Width == rectangle.Height : Console.WriteLine($"SIZE={rectangle.Width}"); break; case Rectangle rectangle : Console.WriteLine($"WIDTH={rectangle.Width},HEIGHT={rectangle.Height}"); break; default : Console.WriteLine(shape.ToString()); break; } } } #endregion /// <summary> /// 도형 /// </summary> public class Shape { } /// <summary> /// 원 /// </summary> public class Circle : Shape { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 반경 - Radius /// <summary> /// 반경 /// </summary> public double Radius { get; set; } #endregion } /// <summary> /// 사각형 /// </summary> public class Rectangle : Shape { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public double Width { get; set; } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public double Height { get; set; } #endregion } |
■ switch문에서 상수 패턴을 사용하는 방법을 보여준다. ▶ 예제 코드 (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 76 77 78 79 80 81 82 83 84
|
using System; using System.Collections.Generic; #region 도형 출력하기 - PrintShape(shapeList) /// <summary> /// 도형 출력하기 /// </summary> /// <param name="shapeList">도형 리스트</param> private void PrintShape(List<Shape> shapeList) { foreach(Shape shape in shapeList) { switch(shape) { case null : Console.WriteLine("NULL"); break; default : Console.WriteLine(shape.ToString()); break; } } } #endregion /// <summary> /// 도형 /// </summary> public class Shape { } /// <summary> /// 원 /// </summary> public class Circle : Shape { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 반경 - Radius /// <summary> /// 반경 /// </summary> public double Radius { get; set; } #endregion } /// <summary> /// 사각형 /// </summary> public class Rectangle : Shape { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 너비 - Width /// <summary> /// 너비 /// </summary> public double Width { get; set; } #endregion #region 높이 - Height /// <summary> /// 높이 /// </summary> public double Height { get; set; } #endregion } |
■ is 연산자에서 타입 패턴(Type Pattern)을 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
using System; using System.Drawing; object[] sourceArray = { 1, null, 10, new Point(5, 10), new Rectangle(10, 10, 200, 200), string.Empty }; foreach(object source in sourceArray) { if(source is Point point) { Console.WriteLine($"X={point.X},Y={point.Y}"); } else if(source is Rectangle rectangle) { Console.WriteLine($"X={rectangle.X},Y={rectangle.Y},WIDTH={rectangle.Width},HEIGHT={rectangle.Height}"); } } |
■ 튜플 분해(Tuple Deconstruction)를 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
List<int> sourceList = new List<int> { 1, 2, 3, 4, 5 }; (int count, int summary, double average) = Calculate(sourceList); Console.WriteLine($"{count}, {summary}, {average}"); #region 계산하기 - Calculate(sourceList) /// <summary> /// 계산하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <returns>카운트, 합계, 평균</returns> private (int Count, int Summary, double Average) Calculate(List<int> sourceList) { int count = 0; int summary = 0; double average = 0d; foreach(int source in sourceList) { count++; summary += source; } average = summary / count; return (count, summary, average); } #endregion |
■ is 연산자에서 상수 패턴(Constant Pattern)을 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; using System.Drawing; object[] sourceArray = { 1, null, 10, new Point(5, 10), new Rectangle(10, 10, 200, 200), string.Empty }; foreach(object source in sourceArray) { if(source is null) { Console.WriteLine("NULL"); } else if(source is 10) { Console.WriteLine("10"); } else if(source is "") { Console.WriteLine("EMPTY STRING"); } else { Console.WriteLine(source.ToString()); } } |
■ 튜플 리터럴(Tuple Literal)을 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; using System.Collections.Generic; List<int> sourceList = new List<int> { 1, 2, 3, 4, 5 }; var result = Calculate(sourceList); Console.WriteLine($"{result.Count}, {result.Summary}, {result.Average}"); Console.WriteLine($"{result.Item1}, {result.Item2}, {result.Item3}"); #region 계산하기 - Calculate(sourceList) /// <summary> /// 계산하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <returns>카운트, 합계, 평균</returns> private (int Count, int Summary, double Average) Calculate(List<int> sourceList) { int count = 0; int summary = 0; double average = 0d; foreach(int source in sourceList) { count++; summary += source; } average = summary / count; return (count, summary, average); } #endregion |
■ 튜플 반환 타입(Tuple Return Type)을 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; using System.Collections.Generic; List<int> sourceList = new List<int> { 1, 2, 3, 4, 5 }; var result = Calculate(sourceList); Console.WriteLine($"{result.Count}, {result.Summary}, {result.Average}"); Console.WriteLine($"{result.Item1}, {result.Item2}, {result.Item3}"); #region 계산하기 - Calculate(sourceList) /// <summary> /// 계산하기 /// </summary> /// <param name="sourceList">소스 리스트</param> /// <returns>카운트, 합계, 평균</returns> private (int Count, int Summary, double Average) Calculate(List<int> sourceList) { int count = 0; int summary = 0; double average = 0d; foreach(int source in sourceList) { count++; summary += source; } average = summary / count; return (count, summary, average); } #endregion |
■ 로컬 함수(Local Function)를 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; Console.WriteLine(Calculate(10, 15, 20)); #region 계산하기 - Calculate(a, b, c) /// <summary> /// 계산하기 /// </summary> /// <param name="a">A</param> /// <param name="b">B</param> /// <param name="c">C</param> /// <returns>계산 값</returns> private int Calculate(int a, int b, int c) { int factor = 10; int ab = Calculate(a, b); int ac = Calculate(a, c); return Math.Max(ab, ac); int Calculate(int x, int y) { int result = 2 * x + 7 * y - 5; return result % factor; } } #endregion |
■ 로컬 함수를 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; using System.Collections.Generic; foreach(int number in GetNumberEnumerable(100, n => n % 2 == 0)) { Console.WriteLine(number); } #region 숫자 열거 가능형 구하기 - GetNumberEnumerable(count, filterFunction) /// <summary> /// 숫자 열거 가능형 구하기 /// </summary> /// <param name="count">카운트</param> /// <param name="filterFunction">필터 함수</param> /// <returns>숫자 열거 가능형</returns> private IEnumerable<int> GetNumberEnumerable(int count, Func<int, bool> filterFunction) { if(count < 1) { throw new ArgumentException(); } if(filterFunction == null) { throw new ArgumentException(); } return GetNumberEnumerable(); IEnumerable<int> GetNumberEnumerable() { for(int i = 0; i < count; i++) { if(filterFunction(i)) { yield return i; } } } } #endregion |
■ out 키워드에서 밑줄(_)을 사용해 out 매개 변수를 생략하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
using System; GetXY(out int x, out _); Console.WriteLine($"{x}"); #region X/Y 구하기 - GetXY(x, y) /// <summary> /// X/Y 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> private void GetXY(out int x, out int y) { x = 5; y = 10; } #endregion |
■ out 키워드를 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
using System; GetXY(out int x, out int y); Console.WriteLine($"{x},{y}"); #region X/Y 구하기 - GetXY(x, y) /// <summary> /// X/Y 구하기 /// </summary> /// <param name="x">X</param> /// <param name="y">Y</param> private void GetXY(out int x, out int y) { x = 5; y = 10; } #endregion |
■ return ref 키워드를 사용해 참조 반환을 하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; NumberData numberData = new NumberData(); Console.WriteLine($"원본 시퀀스 : {numberData.ToString()}"); int number = 16; ref int value = ref numberData.FindNumber(number); value *= 2; Console.WriteLine($"신규 시퀀스 : {numberData.ToString()}"); /// <summary> /// 숫자 데이터 /// </summary> public class NumberData { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 숫자 배열 /// </summary> private int[] numberArray = { 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023 }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 숫자 찾기 - FindNumber(target) /// <summary> /// 숫자 찾기 /// </summary> /// <param name="target">타겟</param> /// <returns>숫자</returns> public ref int FindNumber(int target) { for(int i = 0; i < numberArray.Length; i++) { if(numberArray[i] >= target) { return ref numberArray[i]; } } return ref numberArray[0]; } #endregion #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() => string.Join(" ", this.numberArray); #endregion } |
■ 자리수 분리자 및 이진 리터럴를 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
|
using System; int value1 = 1_000_000; int value2 = 0x10_ff_ff_00; double value3 = 1_000.000_123; int value4 = 0b1001_1111_0000_0101; Console.WriteLine(value1); Console.WriteLine(value2); Console.WriteLine(value3); Console.WriteLine(value4); |
■ ref 키워드에서 참조 로컬을 사용하는 방법을 보여준다. ▶ 예제 코드 (C#)
|
using System; int a = 1; ref int b = ref a; b = 2; Console.WriteLine($"{a}, {b}"); |
■ 분해자(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
■ ValueTask<T> 클래스를 사용하는 방법을 보여준다. ▶ 예제 코드 (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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
using System; using System.Collections.Generic; using System.Threading.Tasks; /// <summary> /// 캐시 헬퍼 /// </summary> public class CacheHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 딕셔너리 /// </summary> private Dictionary<int, int> dictionary; /// <summary> /// 난수기 /// </summary> private Random random; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CacheHelper() /// <summary> /// 생성자 /// </summary> public CacheHelper() { this.dictionary = new Dictionary<int, int>(); this.random = new Random(DateTime.Now.Millisecond); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 값 구하기 (비동기) - GetValueAsync(index) /// <summary> /// 값 구하기 (비동기) /// </summary> /// <param name="index">인덱스</param> /// <returns>값 태스크</returns> public async ValueTask<int> GetValueAsync(int index) { if(this.dictionary.ContainsKey(index)) { return this.dictionary[index]; } else { int value = await FetchValueAsync(index); this.dictionary.Add(index, value); return value; } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 값 꺼내기 (비동기) - FetchValueAsync(index) /// <summary> /// 값 꺼내기 (비동기) /// </summary> /// <param name="index">인덱스</param> /// <returns>값 태스크</returns> private async Task<int> FetchValueAsync(int index) { return await Task<int>.Run ( () => { int value = this.random.Next(1000); return value; } ); } #endregion } |
※ 누겟 설치 : System.Threading.Tasks.Extensions
■ Expression-Bodied 멤버를 소멸자에서 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; /// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID /// </summary> private int id; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(id) /// <summary> /// 생성자 /// </summary> /// <param name="id">ID</param> public Employee(int id) => this.id = id; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Destructor #region 소멸자 - ~Employee() /// <summary> /// 소멸자 /// </summary> ~Employee() => Console.WriteLine("~Employee"); #endregion } |
■ Expression-Bodied 멤버를 이벤트에서 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System; using System.Collections.Generic; /// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 통지시 이벤트 - Notified /// <summary> /// 통지시 이벤트 /// </summary> public event EventHandler Notified { add => this.notified += value; remove => this.notified -= value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 통지시 이벤트 핸들러 /// </summary> private EventHandler notified; /// <summary> /// ID /// </summary> private int id; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(id) /// <summary> /// 생성자 /// </summary> /// <param name="id">ID</param> public Employee(int id) => this.id = id; #endregion } |
■ Expression-Bodied 멤버를 인덱서에서 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
using System.Collections.Generic; /// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID /// </summary> private int id; /// <summary> /// 태그 리스트 /// </summary> private List<string> tagList = new List<string>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value; } #endregion #region 인덱서 - this[index] /// <summary> /// 인덱서 /// </summary> /// <param name="index">인덱서</param> /// <returns>태그</returns> public string this[int index] { get => this.tagList[index]; set => this.tagList[index] = value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(id) /// <summary> /// 생성자 /// </summary> /// <param name="id">ID</param> public Employee(int id) => this.id = id; #endregion } |
■ Expression-Bodied 멤버를 생성자에서 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
/// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID /// </summary> private int id; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(id) /// <summary> /// 생성자 /// </summary> /// <param name="id">ID</param> public Employee(int id) => this.id = id; #endregion } |
■ Expression-Bodied 멤버를 속성에서 사용하는 방법을 보여준다. ▶ 예제 코드 (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
|
/// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID /// </summary> private int id; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID - ID /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Employee(id) /// <summary> /// 생성자 /// </summary> /// <param name="id">ID</param> public Employee(int id) => this.id = id; #endregion } |
■ 널 병합 연산자에서 예외를 발생시키는 방법을 보여준다. ▶ 예제 코드 (C#)
|
using System; string temporaryName = null; ... string name = temporaryName ?? throw new ArgumentException("temporaryName"); |
■ Expression-Bodied 멤버에서 예외를 발생시키는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
using System; /// <summary> /// ID /// </summary> private int id; /// <summary> /// ID /// </summary> public int ID { get => this.id; set => this.id = value > 0 ? value : throw new ArgumentException(); } |