12. ๊ตฌ์กฐ์ฒด & ํŠœํ”Œ
ยท
๐Ÿ’ป Programming Language/C#
๊ตฌ์กฐ์ฒด(Struct) => ํ‚ค์›Œ๋“œ : struct ์‚ฌ์šฉ using System; namespace StructEx { //๊ตฌ์กฐ์ฒด ์ •์˜ํ•˜๊ธฐ struct MyPoint { public int x; public int y; //**๊ตฌ์กฐ์ฒด๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋Š” ์„ ์–ธํ•  ์ˆ˜ ์—†๋‹ค. public MyPoint(int x, int y) { this.x = x; this.y = y; } // ๋ชจ๋“  ๊ตฌ์กฐ์ฒด๋Š” System.Object ํ˜•์‹์„ ์ƒ์†ํ•˜๋Š” System.ValueType์œผ๋กœ ๋ถ€ํ„ฐ ์ง์ ‘ ์ƒ์†๋ฐ›์Œ. public override string ToString() { return string.Format($"{x} {y}"); } } class Program { static void Main(string[] arg..
11. ์˜ค๋ฒ„๋ผ์ด๋”ฉ(virtual, override, new)
ยท
๐Ÿ’ป Programming Language/C#
namespace Override { class BaseClass { public virtual void echo() { Console.WriteLine("๊ธฐ๋ฐ˜ ํด๋ž˜์Šค"); } public virtual void echo2() { Console.WriteLine("๊ธฐ๋ฐ˜ echo2"); } } class DerivedClass : BaseClass { override public void echo() //๋ถ€๋ชจํด๋ž˜์Šค ๋‚ด์— virtual๋กœ ์„ ์–ธ๋˜์–ด ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ํŒŒ์ƒํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ํ• ๋•Œ // override ํ‚ค์›Œ๋“œ๋ฅผ ์•ž์— ๋ถ™์ด๊ณ  ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ํ•ด์ค€๋‹ค. // => ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š”๊ฒŒ ์ •ํ™•ํ•œ ๋ฐฉ๋ฒ•์ด๊ณ  ์—๋Ÿฌ๋‚˜ ๊ฒฝ๊ณ ๊ฐ€ ์•ˆ๋œธ { Console.WriteLine("ํŒŒ์ƒ ํด๋ž˜์Šค"); } public new void echo..
10. is ์—ฐ์‚ฐ์ž์™€ as ์—ฐ์‚ฐ์ž
ยท
๐Ÿ’ป Programming Language/C#
namespace AsisEx { class Human { public void showInfo() { Console.WriteLine("Human"); } } class Programmer : Human { public void programming() { Console.WriteLine("๊ฐœ๋ฐœ์ž"); } } class Designer : Human { public void design() { Console.WriteLine("๋””์ž์ด๋„ˆ"); } } class Program { static void Main(string[] args) { Human human = new Programmer(); //ํŒŒ์ƒํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ๊ธฐ๋ฐ˜ํด๋ž˜์Šค ์ธ์Šคํ„ด์Šค๋กœ ์„ ์–ธ Programmer programmer; if (human ..
09. ์ƒ์†๊ด€๊ณ„์˜ ํด๋ž˜์Šค ํ˜•๋ณ€ํ™˜
ยท
๐Ÿ’ป Programming Language/C#
namespace ClassConversionEx { // ์ƒ์†๊ด€๊ณ„์˜ ํด๋ž˜์Šค ์‚ฌ์ด์—์„œ์˜ ํ˜•๋ณ€ํ™˜ class Human { public void showInfo() { Console.WriteLine("Human"); } } class Programmer : Human { public void programming() { Console.WriteLine("ํ”„๋กœ๊ทธ๋ž˜๋ฐ ํ•˜๋Š” ๊ฐœ๋ฐœ์ž"); } } class Designer : Human { public void design() { Console.WriteLine("๋””์ž์ธํ•˜๋Š” ๋””์ž์ด๋„ˆ"); } } class Program { static void Main(string[] args) { Human human = new Human(); human.showInfo(); ..
08. baseํ‚ค์›Œ๋“œ์™€ sealedํ•œ์ •์ž
ยท
๐Ÿ’ป Programming Language/C#
namespace inheritEx2 { // ๊ธฐ๋ฐ˜(๋ถ€๋ชจ) ํด๋ž˜์Šค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž์—๊ฒŒ // ํŒŒ์ƒ(์ž์‹) ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž์—์„œ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ• /* C# ์—์„œ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค ์ƒ์„ฑ์ž ์ค‘์—์„œ ์‚ฌ์šฉ์ž ์ง€์ • ์ƒ์„ฑ์ž(๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์žˆ๋Š” ์ƒ์„ฑ์ž)๊ฐ€ ์กด์žฌ ํ•  ๋•Œ ๋ถ€๋ชจ ํด๋ž˜์Šค ์ƒ์„ฑ์ž์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ (Ex. ๋งค๊ฐœ๋ณ€์ˆ˜ = p) ๋ฅผ ์ž์‹ํด๋ž˜์Šค์—์„œ ์ „๋‹ฌํ•˜์—ฌ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฆ‰, ์ž์‹ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค(๋ถ€๋ชจํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š”๊ฒฝ์šฐ)๋ฅผ ์ƒ์„ฑํ• ๋•Œ ๋ถ€๋ชจํด๋ž˜์Šค์— ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜์—ฌ ์ดˆ๊ธฐํ™” ํ•  ์ˆ˜ ์žˆ์Œ. cf) Java? Python์ธ๊ฐ€? ๊ฑฐ๊ธฐ์„œ ๋ดค๋˜ super() ๊ฐ™์€ ๊ฒƒ์ธ๋“ฏํ•จ. */ /* sealed */ class Parent // ์ด์™€ ๊ฐ™์ด `sealed` ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด // ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œ์ผœ ์ƒ์†์„ ๋ถˆ๊ฐ€๋Šฅ..
07. ์ƒ์†
ยท
๐Ÿ’ป Programming Language/C#
namespace inheritEx { class Parent // ์ƒ์†์„ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ฅผ ๋ถ€๋ชจ, ์Šˆํผ, ์ƒ์œ„ ํด๋ž˜์Šค๋ผ๊ณ  ์ผ์ปซ๋Š”๋‹ค. // ๊ธฐ๋ฐ˜(Base) ํด๋ž˜์Šค๋ผ๊ณ ๋„ ๋ถ€๋ฅธ๋‹ค. { public Parent() { Console.WriteLine("Parent ์ƒ์„ฑ์ž ํ˜ธ์ถœ"); } ~Parent() { Console.WriteLine("Parent ์ข…๋ฃŒ์ž ํ˜ธ์ถœ"); } public void showInfo() { Console.WriteLine("Parent Class"); } } class Child : Parent // ์ƒ์†์„ ๋ฐ›๋Š” ํด๋ž˜์Šค๋ฅผ ์ž์‹(ํŒŒ์ƒ)ํด๋ž˜์Šค๋ผ๊ณ  ์ผ์ปซ๋Š”๋‹ค. // ์ƒ์†์„ ๋ฐ›์„ ๋•Œ์—๋Š” : ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์†๋ฐ›๋Š”๋‹ค. // ์ƒ์†๋ฐ›์€ ์ฝ”๋“œ๋ฅผ ์žฌํ™œ์šฉํ•ด์„œ ์ž์‹ ์—๊ฒŒ ๋งž๋Š” ์ฝ”๋“œ๋กœ ์ˆ˜์ •ํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ..
06. ์ ‘๊ทผ์ œํ•œ์ž(Access Modifier)
ยท
๐Ÿ’ป Programming Language/C#
namespace AccessModifierEx { class Demo { // public ์ ‘๊ทผ ์ œํ•œ์ž public static void public_method() { } // private ์ ‘๊ทผ ์ œํ•œ์ž private static void private_method() { } // protected ์ ‘๊ทผ ์ œํ•œ์ž protected static void protected_method() { } // internal ์ ‘๊ทผ ์ œํ•œ์ž internal static void internal_method() { } // protected internal ์ ‘๊ทผ ์ œํ•œ์ž protected internal static void protected_internal_method() { } // private protected ์ ‘..