36. ์ต๋ช…๋ฉ”์†Œ๋“œ
ยท
๐Ÿ’ป Programming Language/C#
์ต๋ช…๋ฉ”์†Œ๋“œ(Anonymous Method) : ์ด๋ฆ„์ด ์—†๋Š” ๋ฉ”์†Œ๋“œ ์„ ์–ธํ˜•์‹ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์ธ์Šคํ„ด์Šค๋ช… = delegate(๋งค๊ฐœ๋ณ€์ˆ˜) { ์‹คํ–‰์ฝ”๋“œ ... } ์‚ฌ์šฉ ์˜ˆ์ œ ์ฝ”๋“œ using System.Collections; namespace AnonymousMethodEx { delegate int CalDelegate(int x, int y); class Program { static void Main(string[] args) { CalDelegate cd; cd = delegate (int x, int y) { return x * y; }; // => ์ต๋ช…๋ฉ”์†Œ๋“œ ํ˜•ํƒœ (์ด๋ฆ„์ด ์—†์Œ) // ๋‹จ ๋Œ€๋ฆฌ์ž์˜ ํ˜•์‹๊ณผ ๊ฐ™์€ ํ˜•์‹์„ ์จ์•ผํ•œ๋‹ค. // ๊ณ ๋ คํ•ด์•ผ ํ•  ํ˜•์‹์€ ๋ฐ˜ํ™˜ํƒ€์ž…, ์ธ์žํƒ€์ž…, ์ธ์ž์ˆ˜๊ฐ€ ์žˆ๋‹ค. Console.W..
35. ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์ฒด์ธ(delegate chain)
ยท
๐Ÿ’ป Programming Language/C#
๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์ฒด์ธ(delegate chain) : ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ํ•˜๋‚˜๊ฐ€ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๋™์‹œ์— ์ฐธ์กฐํ•˜๋Š” ๊ฒƒ. ์‚ฌ์šฉ ์˜ˆ์ œ ์ฝ”๋“œ using System.Collections; namespace DelegateEx { delegate void CalDelegate(int x , int y); class Program { static void Add(int x, int y) { Console.WriteLine(x + y); } static void Sub(int x, int y) { Console.WriteLine(x - y); } static void Mul(int x, int y) { Console.WriteLine(x * y); } static void Div(int x, int y) { Console.Wri..
34. ์ œ๋„ค๋ฆญ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(Generic Delegate) & ์ฝœ๋ฐฑํ•จ์ˆ˜
ยท
๐Ÿ’ป Programming Language/C#
์•Œ๋ฉด ์ข‹์€ ์ง€์‹ - Callback Function(์ฝœ๋ฐฑํ•จ์ˆ˜) Delegate๋ฅผ ํ™œ์šฉํ•œ ํ•จ์ˆ˜์ „๋‹ฌ์€ callback๊ณผ ๊ฐ™์€ ๊ฐœ๋… callback : A, B, C ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์„๋•Œ, A๊ฐ€ B(์ค‘๊ฐ„์ž=Delegate)๋ฅผ ํ†ตํ•ด์„œ C์—๊ฒŒ ์ „๋‹ฌ์„ ํ•ด๋‹ฌ๋ผ๊ณ  ํ–ˆ๋Š”๋ฐ, B๊ฐ€ C์—๊ฒŒ A์—๊ฒŒ ์—ฐ๋ฝํ•  ๊ฒƒ์„ ์š”์ฒญํ•˜๊ณ , C๊ฐ€ ๋‹ค์‹œ A์—๊ฒŒ ์—ฐ๋ฝ์„ ์ทจํ•œ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•ด A๋ฅผ C์—๊ฒŒ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์€๋ฐ ์ค‘๊ฐ„์— delegate๊ฐ€ ๊ทธ ์—ญํ• ์„ ํ•˜๊ณ , C๋Š” ๊ฒฐ๊ณผ๋ฅผ A์—๊ฒŒ ๋ฐ˜ํ™˜ํ•œ๋‹ค. using System.Collections; namespace DelegateEx { delegate int CalculationDelegate(int x, int y); class Program { static int Add(int x, int y) { return..
33. ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(delegate) ์‚ฌ์šฉ
ยท
๐Ÿ’ป Programming Language/C#
๋ธ๋ฆฌ๊ฒŒ์ดํŠธ ์‚ฌ์šฉ ์˜ˆ์ œ ์ฝ”๋“œ using System.Collections; namespace DelegateEx { delegate int MyDelegate(int a, int b); class SumSubtract { public int sum(int a, int b) { return a + b; } public static int subtract(int a, int b) // ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋Š” static ๋ฉ”์†Œ๋“œ๋„ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. { return a - b; } // ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(delegate = ๋Œ€๋ฆฌ์ž)๋Š” ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋“ค์ด ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ฃผ์†Œ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. } class Program { static void Main(string[] args) { SumSubtract ss = new SumSub..
32. ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(delegate)์˜ ๊ธฐ๋ณธ ๊ฐœ๋…
ยท
๐Ÿ’ป Programming Language/C#
๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(Delegate) : ๋ฉ”์†Œ๋“œ๋ฅผ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ๋งŒ๋“ค์–ด ์ง„ ๊ฒƒ. => ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ์ด์šฉํ•ด ํฌ์žฅํ•˜๊ณ  ์ „๋‹ฌ.(๋‚ด๊ฐ€ ์ดํ•ดํ•œ๋ฐ”๋กœ๋Š” .. ใ…‡ใ…‡) void AAA(int arg1){ ... } int a = 123; AAA(a); class DemoClass { int id; string name; public int DisplayInfo() {..} } void AAA(DemoClass d){ ... } DemoClass d = new DemoClass(); AAA(d) ์œ„์˜ ์ฝ”๋“œ๋“ค์€ ๊ธฐ์กด ์ธ์ž๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด ํ˜น์€ ๊ฐ’์„ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ์‹ ๋ฉ”์†Œ๋“œ๋ฅผ ์ „๋‹ฌํ•˜๊ณ  ์‹ถ์„๋• delegate๋ฅผ ์‚ฌ์šฉ void AAA(Mydelegate Method){ ... } int StringToInt(str..
31. ์˜ˆ์™ธํ•„ํ„ฐ(Exception Filter)
ยท
๐Ÿ’ป Programming Language/C#
Exception Filter : catch์ ˆ์ด ๋ฐ›์•„๋“ค์ผ ์˜ˆ์™ธ ๊ฐ์ฒด์— ์ œ์•ฝ์‚ฌํ•ญ์„ ์ฃผ๊ณ  ๊ทธ ์‚ฌํ•ญ์— ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ์— ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ catch๋ฌธ ๋’ค์— when ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉ. ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค ๋ชจ๋“  ์˜ˆ์™ธ ๊ฐ์ฒด๋Š” System.Exception ํด๋ž˜์Šค๋กœ๋ถ€ํ„ฐ ํŒŒ์ƒ๋œ๋‹ค. ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๋•Œ๋„ ์—ญ์‹œ System.Exception ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ๋งŒ๋“ ๋‹ค. ์‚ฌ์šฉ ์˜ˆ์ œ ์ฝ”๋“œ using System.Collections; namespace ExceptionEx { // ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ ํด๋ž˜์Šค class UserException : Exception { public int ErrorCode { get; set; } } class Program { static void Mai..
30. C# 7.0์—์„œ throw๋ฌธ ํ‘œํ˜„์‹, finally ์ ˆ
ยท
๐Ÿ’ป Programming Language/C#
C# 7.0์—์„œ์˜ throw๋ฌธ์˜ ํ‘œํ˜„์‹(Expression) using System.Collections; //c# 7.0์—์„œ์˜ throw๋ฌธ์˜ ํ‘œํ˜„์‹(Expression) namespace ExceptionEx { class Program { static void Main(string[] args) { try { int? aa = null; int bb = aa ?? throw new ArgumentNullException(); } catch (ArgumentNullException e) { Console.WriteLine(e.Message); } try { int[] arr = {1, 2, 3 }; int idx = 5; int value = arr[idx >= 0 && idx < 4 ? idx : t..