39. LINQ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ
ยท
๐Ÿ’ป Programming Language/C#
LINQ, ORM => SQL๋ฌธ๊ณผ๋Š” ๋‹ค๋ฅด๊ฒŒ DB๋กœ๋ถ€ํ„ฐ ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ํŽธ๋ฆฌํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ด. LINQ(Language Integrated Query) : C# ์–ธ์–ด์— ํ†ตํ•ฉ๋œ ๋ฐ์ดํ„ฐ ์งˆ์˜ ๊ธฐ๋Šฅ using System.Collections; namespace LinqEx { class Member { public string name { get; set; } public int age { get; set; } } class Program { static void Main(string[] args) { Member[] ml = { new Member(){name = "ํ™๊ธธ๋™" , age =23}, new Member(){name = "์ฒญ๊ธธ๋™" , age =24}, new Member(){name = "๋ฐฑ๊ธธ๋™" , ..
38. ๋žŒ๋‹ค์‹(Lambda Expression)
ยท
๐Ÿ’ป Programming Language/C#
๋žŒ๋‹ค์‹ : ์ต๋ช… ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ์‹ ๋žŒ๋‹ค์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ต๋ช… ๋ฉ”์†Œ๋“œ๋ฅผ ๋ฌด๋ช… ํ•จ์ˆ˜(Anonymous Function) ๋ผ๊ณ  ํ•œ๋‹ค. ๋žŒ๋‹ค์‹ ์„ ์–ธ ํ˜•์‹ ๋งค๊ฐœ๋ณ€์ˆ˜ ๋ชฉ๋ก => ์‹ => ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅ ์—ฐ์‚ฐ์ž๋ผ๊ณ  ํ•œ๋‹ค. delegate int AAA(int x, int y); static void Main(string[] args){ AAA aaa = (int x,int y) => x + y; } (int x, int y) => x + y ๋ผ๋Š” ์ต๋ช… ๋ฉ”์†Œ๋“œ๊ฐ€ delegate ์ธ aaa์— ์ „๋‹ฌ๋˜๊ณ  ์žˆ๋Š” ๋ชจ์Šต ์œ„์˜ ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๊ฟ”์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ AAA aaa = (x, y) => x + y; delegate์˜ ์ธ์ž์— ํƒ€์ž…์ด ์„ ์–ธ๋˜์–ด ์žˆ๊ธฐ์— ๊ฐ€๋Šฅ ์œ„์˜ ์ต๋ช… ๋ฉ”์†Œ๋“œ(๋ฌด๋ช…ํ•จ์ˆ˜)๋Š” ๊ธฐ์กด์— ์ต๋ช… ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ..
37. ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ๋ฅผ ํ†ตํ•œ ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ
ยท
๐Ÿ’ป Programming Language/C#
์ด๋ฒคํŠธ : ํŠน์ •ํ•œ ์ผ์ด ์ผ์–ด๋‚ฌ์Œ์„ ์•Œ๋ ค์ฃผ๋Š” ๊ธฐ๋Šฅ C# ์—์„œ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ธ๋ฆฌ๊ฒŒ์ดํŠธ(delegate)์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•œ๋‹ค. event ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ‘œ์‹œํ•œ๋‹ค. ์ด๋ฒคํŠธ ์„ ์–ธ๊ณผ ์‚ฌ์šฉ ์ˆœ์„œ 1. delegate๋ฅผ ์„ ์–ธ 2. ์„ ์–ธ๋œ delegate์— event ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ค€๋‹ค. 3. event handler๋ฅผ ์ž‘์„ฑํ•œ๋‹ค. ์„ ์–ธ๋œ delegate์˜ ํ”„๋กœํ† ํƒ€์ž…๊ณผ ๋™์ผํ•˜๊ฒŒ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž‘์„ฑ. => ์ด๋•Œ event handler๋Š” delegate ์„ ์–ธ์‹œ์— ํ”„๋กœํ† ํƒ€์ž…๊ณผ ๋™์ผํ•œ ๋ฉ”์†Œ๋“œํ˜•ํƒœ๋กœ ์ž‘์„ฑํ•œ๋‹ค. 4. ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์ด ๊ฐ์ฒด์— ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ๋“ฑ๋ก 5. ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ˜ธ์ถœ using System.Collections; namespace EventEx { delegate void EventHa..
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..