26. ์ œ๋„ค๋ฆญ์Šค(Generics) ํƒ€์ž… ์ œ์•ฝ
ยท
๐Ÿ’ป Programming Language/C#
์ œ๋„ค๋ฆญ์Šค ํƒ€์ž… ์ œ์•ฝ ์•ž์—์„œ ๋‚˜์˜จ ์ผ๋ฐ˜ํ™” ๋ฉ”์†Œ๋“œ, ์ผ๋ฐ˜ํ™” ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉํ•˜๋˜ T(Type Parameter)๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ ํ˜•์‹์„ ๋Œ€์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค. ๋ชจ๋“  ํ˜•์‹์„ ๋Œ€์‹ ํ•˜๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”ํ•  ์ˆ˜๋„ ์žˆ์ง€๋งŒ ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ๋„ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋Ÿฐ ๊ฒฝ์šฐ์—๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ œ์•ฝ ์กฐ๊ฑด์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ์ œ์•ฝ ์กฐ๊ฑด ์ฃผ๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ where์ ˆ์„ ์ด์šฉํ•œ๋‹ค. Ex) class DemoList where T : DemoClass { ... } ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ์— T๋ผ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” "DemoClass๋กœ ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›๋Š” ํ˜•์‹์ด์–ด์•ผ ํ•œ๋‹ค" ๋ผ๋Š” ์˜๋ฏธ void CopyData(T a, T b) where T : struct // struct๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ value type(๊ฐ’ ํ˜•์‹) { ... } ์œ„์™€ ๊ฐ™์€ ๊ฒฝ์šฐ T๋Š”..
25. ์ œ๋„ค๋ฆญ์Šค(Generics)
ยท
๐Ÿ’ป Programming Language/C#
์ œ๋„ค๋ฆญ์Šค(Generics) : int, float, double ๋“ฑ๊ณผ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ํ™•์ •ํ•˜์ง€ ์•Š๊ณ , ์ด ๋ฐ์ดํ„ฐ ํƒ€์ž… ์ž์ฒด๋ฅผ ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ(Type Parameter)๋กœ ๋ฐ›์•„๋“ค์ด๋„๋ก ํ•˜๋Š” ๊ธฐ๋Šฅ ์ œ๋„ค๋ฆญ์Šค(Generics)๋ผ๊ณ  ํ•˜๋ฉด ์ผ๋ฐ˜ํ™”๋ผ๊ณ  ํ”ํžˆ ์•Œ๋ ค์ ธ์žˆ๋‹ค. Generic ๋ฉ”์†Œ๋“œ ์„ ์–ธ ๋ฐฉ์‹ ์ ‘๊ทผ์ œํ•œ์ž ๋ฐ˜ํ™˜ํ˜•์‹ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„ (๋งค๊ฐœ๋ณ€์ˆ˜) { ... } Ex) void CopyData(int a, int b) { b = a; b์— a๋ฅผ ๋ณต์‚ฌ } void CopyData(string a, string b) { b = a; b์— ๋ฌธ์ž์—ด a๋ฅผ ๋ณต์‚ฌ } ... ๋‹ค๋ฅธ ํƒ€์ž…์— ๋Œ€ํ•œ CopyData ๋ฉ”์†Œ๋“œ๋“ค... ์œ„์˜ ๋ฐฉ์‹์€ ๋ฐ์ดํ„ฐ ํ˜•์‹์— ๋”ฐ๋ผ์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ๋น„ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ• ์œ„์˜ ๋ฐฉ์‹์„ Generic ..
24. yield ํ‚ค์›Œ๋“œ
ยท
๐Ÿ’ป Programming Language/C#
Enumerator(Iterator) : ์ง‘ํ•ฉ์ ์ธ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ํ˜ธ์ถœ์ž์—๊ฒŒ ๋ณด๋‚ด์ฃผ๊ฒŒ ํ•˜๋Š” ๊ธฐ๋Šฅ => ๋ฐ˜๋ณต์ž yield ํ‚ค์›Œ๋“œ๋Š” ํ˜ธ์ถœ์ž์—๊ฒŒ ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฆฌํ„ดํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ yield ์‚ฌ์šฉ๋ฐฉ์‹ yield return : ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ์‚ฌ์šฉ yield break : ๋ฆฌํ„ด์„ ์ค‘์ง€ํ•˜๊ณ  Iteration ๋ฃจํ”„๋ฅผ ๋น ์ ธ๋‚˜์˜ฌ๋•Œ ์‚ฌ์šฉ IEnumerator == ๋ฐ์ดํ„ฐ๋ฅผ ๋ฆฌํ„ด(Getter)ํ•˜๋Š” ์—ด๊ฑฐ์ž public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); } Current ์ฝ๊ธฐ ์ „์šฉ ํ”„๋กœํผํ‹ฐ๋กœ ํ˜„์žฌ ์œ„์น˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ object ํƒ€์ž…์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค. object๋Š” System..
23. ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐํ™”, ์ธ๋ฑ์„œ
ยท
๐Ÿ’ป Programming Language/C#
ArrayList, Stack, Queue using System.Collections; using static System.Console; //์ด์™€ ๊ฐ™์ด using static System.Console; ์„ ์„ ์–ธํ•˜๋ฉด ๋ณธ๋ฌธ์—์„œ Console์„ ์ œ์™ธํ•˜๊ณ  // WriteLine()๋งŒ์œผ๋กœ ์ถœ๋ ฅ๋ฌธ์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค. namespace CollectionInitialEx { class Program { static void Main(string[] args) { //์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• => Stack, Queue์—์„œ๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค. //Stack์ด๋‚˜ Queue๋Š” Add๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ธฐ๋•Œ๋ฌธ //์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋Š” IEnumerable์ด๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„ Add()๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ..
22. Queue, Stack, Hashtable ์‚ฌ์šฉ
ยท
๐Ÿ’ป Programming Language/C#
Queue(ํ) Queue : ์„ ์ž…์„ ์ถœ(first in first out(FIFO)) ํ˜•ํƒœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ Enqueue : ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ Dequque : ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅ using System.Collections; namespace QueueEx { class Program { static void Main(string[] args) { Queue q = new Queue(); q.Enqueue(1); q.Enqueue(100); q.Enqueue(200); q.Dequeue(); q.Enqueue(22.5); int aa = (int)q.Dequeue(); // objํƒ€์ž…์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋ฏ€๋กœ casting ํ•„์š”. q.Enqueue("๊ฐ€๋‚˜๋‹ค"); while(q.Count > 0) { Console.WriteLi..
21. ArrayList ์‚ฌ์šฉํ•˜๊ธฐ
ยท
๐Ÿ’ป Programming Language/C#
Collection(์ปฌ๋ ‰์…˜) : ๊ฐ„๋‹จํžˆ ๋งํ•˜๋ฉด, ๋ฐ์ดํ„ฐ ๋ชจ์Œ์„ ๋‹ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ ๋ฐฐ์—ด์ด๋‚˜, ์Šคํƒ, ํ ๋“ฑ์„ ์ปฌ๋ ‰์…˜์ด๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ œ๊ณต ๋ฐฐ์—ด์€ System.Array ํƒ€์ž…์ด๋‹ค.(=System.Arrayํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.) => System.Array๋Š” ICollection ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์† => ๋”ฐ๋ผ์„œ ๋ฐฐ์—ด๋„ ์ปฌ๋ ‰์…˜(collection)์˜ ์ผ๋ถ€์ด๋‹ค. .Net ํ”„๋ ˆ์ž„์›Œํฌ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ปฌ๋ ‰์…˜์€ ICollection ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค. ArrayList, Queue, Stack, Hashtable ArrayList ArrayList๋Š” ๋ฐฐ์—ด๊ณผ ๋น„์Šทํ•œ ์ปฌ๋ ‰์…˜ ๋ฐฐ์—ด์ฒ˜๋Ÿผ []์ธ๋ฑ์Šค๋กœ ์š”์†Œ์˜ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜๊ณ , ํŠน์ • ์š”์†Œ๋ฅผ ๋ฐ”๋กœ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ๋‹ค.ํ•˜์ง€๋งŒ, ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ๋•Œ๋Š” ๋ฐฐ์—ด ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐ˜๋ฉด, ArrayLis..
20. ์ถ”์ƒํด๋ž˜์Šค์—์„œ ์ž๋™ ํ”„๋กœํผํ‹ฐ ์‚ฌ์šฉ๋ฒ•
ยท
๐Ÿ’ป Programming Language/C#
namespace AbstractEx { abstract class Book { private static int isbn = 0; public string ISBN { get { return String.Format("{0:d4}", isbn++); } } // ์ถ”์ƒ ํ”„๋กœํผํ‹ฐ => ๊ตฌํ˜„์ด ์•ˆ๋œ ํ”„๋กœํผํ‹ฐ abstract๊ฐ€ ๋ถ™์–ด์žˆ๊ธฐ ๋•Œ๋ฌธ์— abstract public DateTime BookDate { get;set; } } class MyBook : Book { public override DateTime BookDate { get; set; } } class Program { static void Main(string[] args) { Book book1 = new MyBook() { BookDate ..