๐ŸŽฎ Unity Study/C#

[KLA ๋ฉ€ํ‹ฐ์บ ํผ์Šค] C#7.0 ์ œ๋Œ€๋กœ ๋ฐฐ์šฐ๊ธฐ Part.2(์ž…๋ฌธ2)

ibelieveinme 2022. 8. 4. 12:39
728x90

22.08.04(๋ชฉ)

[1~2์ฐจ์‹œ - ์ƒ์„ฑ์ž(Constructor)์™€ ์ข…๋ฃŒ์ž]

 

* ์ƒ์„ฑ์ž(Constructor)

: ์„ค๊ณ„๋œ ํด๋ž˜์Šค ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์†Œ๋“œ์™€ ๊ฐ™์€ ๊ฒƒ.

 

* ์ƒ์„ฑ์ž ํ˜•์‹

ํ•œ์ •์ž ํด๋ž˜์Šค๋ช… (๋งค๊ฐœ๋ณ€์ˆ˜๋ช…) 
{

}

 

* ์ƒ์„ฑ์ž ํŠน์ง•

- ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ๋•Œ ์ƒ์„ฑ์ž๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ, ์ปดํŒŒ์ผ๋Ÿฌ์—์„œ ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

- ์ƒ์„ฑ์ž์˜ ์ด๋ฆ„์€ ํด๋ž˜์Šค์˜ ์ด๋ฆ„๊ณผ ๊ฐ™๋‹ค.

 

* ์ข…๋ฃŒ์ž

: ๊ฐ์ฒด์— ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ•ด์ œํ•˜๋Š” ๊ฒƒ.

 

* ์ข…๋ฃŒ์ž ํ˜•์‹

~ํด๋ž˜์Šค๋ช… () 
{

}

 

* ์ข…๋ฃŒ์ž ํŠน์ง•

- ํด๋ž˜์Šค๋ช…์— ~๋ฅผ ๋ถ™์—ฌ ์‚ฌ์šฉํ•œ๋‹ค.

- ์ƒ์„ฑ์ž์™€ ๋‹ฌ๋ฆฌ ํ•œ์ •์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค.

- ๋งค๊ฐœ๋ณ€์ˆ˜๋„ ์—†๋‹ค. ์˜ค๋ฒ„๋กœ๋”ฉ๋„ ์•ˆ๋จ.

- ์ง์ ‘ ํ˜ธ์ถœ์ด ๋ถˆ๊ฐ€ํ•˜๊ณ  CLR(Common Language Runtime)์˜ ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰ํ„ฐ๊ฐ€ ๊ฐ์ฒด ์†Œ๋ฉธ ์‹œ์ ์„ ํŒ๋‹จํ•ด์„œ ์ข…๋ฃŒ์ž๋ฅผ ํ˜ธ์ถœํ•ด์ค€๋‹ค.

- CLR ๋•๋ถ„์— ์ข…๋ฃŒ์ž ๊ตฌ์ง€ ์•ˆ์ ์–ด๋„ ๋จ.

 

* CLR(Common Language Runtime) ? โญ๏ธโญ๏ธโญ๏ธ

: ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์‹คํ–‰ํ™˜๊ฒฝ์„ ์ œ๊ณตํ•ด์ฃผ๋Š” ๊ฒƒ.

: CLR์ด C# ์ฝ”๋“œ๋ฅผ ์ฝ์–ด์„œ ์ปดํ“จํ„ฐ(OS)๊ฐ€ ์ดํ•ดํ•  ์ˆ˜ ์žˆ๋Š” Native Code ๋กœ ๋ณ€์—ญํ•ด์ฃผ๋Š” ์—ญํ• . ( JIT ์ปดํŒŒ์ผ )

cf) C# 6.0์—์„œ๋Š” ํฌ๋งทํŒ…์„ ํ•  ๋•Œ $ ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•ด์„œ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค.

22.08.05(๊ธˆ)

[3์ฐจ์‹œ - static ํ•„๋“œ์™€ static ๋ฉ”์†Œ๋“œ]

* static: ์ •์ ์˜ ์˜๋ฏธ. ex) ์ •์  ๋ฉ”์†Œ๋“œ, ์ •์  ํ•„๋“œ(๋ณ€์ˆ˜) ๋“ฑ

 

ํ•œ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์— ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜๋งŒ ์กด์žฌํ•˜์ง€๋งŒ ์ธ์Šคํ„ด์Šค๋Š” ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.

ํ•œ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์— ๋˜‘๊ฐ™์€ ํด๋ž˜์Šค๋Š” ๋‘ ๊ฐœ๊ฐ€ ์กด์žฌํ•  ์ˆ˜ ์—†๋‹ค.

์ด์ฒ˜๋Ÿผ static ํ•„๋“œ๋‚˜ ๋ฉ”์†Œ๋“œ๋Š” ํด๋ž˜์Šค์— ์†Œ์†๋œ ๊ฒƒ์ด๋ฏ€๋กœ ํ”„๋กœ๊ทธ๋žจ ์•ˆ์— ์œ ์ผํ•˜๊ฒŒ ์กด์žฌํ•œ๋‹ค. => Class ๋ช…์œผ๋กœ ์ง์ ‘ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ

namespace StaticEx{

    class Program{
    	static void Main(string[] args){
            DemoClass demoClass = new DemoClass();
            demoCalss.a = 100;
            demoClass.b = 200;
            
            DemoClass demoClass2 = new DemoClass();
            demoClass2.a = 1;
            demoClass2.b = 2;
            
            SdemoClass.a = 11; // static ํ•„๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์—(์œ ์ผํ•œ) ํด๋ž˜์Šค ๋ช…์œผ๋กœ ๋ฐ”๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
            SdemoClass.b = 22;
        }
    }

    class DemoClass{
    	public int a;
        public int b;
    }
    
    class SdemoClass{
    	public static int a;
        public static int b;
    }
}

 

* ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ, ํ•„๋“œ์™€ static ๋ฉ”์†Œ๋“œ, ํ•„๋“œ์˜ ์ฐจ์ด์ 

namespace StaticEx2{
    class MyClass{
    	public static int Global_Count = 0;
    }
    
    class DemoCalss1{
    	public DemoClass2(){
        	MyClass.Galobal_Count++;
        }
    }
    class DemoCalss2{
    	public DemoClass2(){
        	MyClass.Galobal_Count++;
        }
    }
    class Program{
    	static void Main(string[] args){
            Console.WriteLine($"Global_Count: {MyClass.Global_Count}");
            new DemoClass1();
            new DemoClass1();
            new DemoClass2();
            Console.WriteLine($"Global_Count: {MyClass.Global_Count}");
        }
    }
}

 

[4์ฐจ์‹œ - ๊นŠ์€๋ณต์‚ฌ์˜ ์ดํ•ด]

 

ex) ์–•์€ ๋ณต์‚ฌ, ๊นŠ์€ ๋ณต์‚ฌ ์˜ˆ์‹œ ์ฝ”๋“œ

namespace ObjCopyEx{
    class Program{
    	class Demo1{
            public int aa;
            public int bb;
            
            //๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ์œ„ํ•œ ๋ฉ”์†Œ๋“œ
            public Demo1 DeepCopy(){
            	Demo1 newDemo1 = new Demo1();
                newDemo1.aa = this.aa;
                newDemo1.bb = this.bb;
                
                return newDemo1;
            }
        }
        static void Main(string[] args){
            //์–•์€ ๋ณต์‚ฌ
            Demo1 demo1 = new Demo1();
            demo1.aa = 100;
            demo1.bb = 100;
            
            Demo1 demo11 = demo1;
            demo11.bb = 1111;
            
            Console.WriteLine("----------์–•์€ ๋ณต์‚ฌ----------")
            Console.WriteLine($"{demo1.aa} {demo1.bb}");//100 1111
            Console.WriteLine($"{demo11.aa} {demo11.bb}");//100 1111
            
            //๊นŠ์€ ๋ณต์‚ฌ
            Demo1 demo1 = new Demo1();
            demo1.aa = 100;
            demo.bb = 1000;
            
            Demo1 demo11 = demo1.DeepCopy();
            demo11.bb = 1111;
            
            Console.WriteLine("----------๊นŠ์€ ๋ณต์‚ฌ----------")
            Console.WriteLine($"{demo1.aa} {demo1.bb}");//100 1000
            Console.WriteLine($"{demo1.aa} {demo1.bb}");//100 1111
        }
    }
}

 

์–•์€ ๋ณต์‚ฌ, ๊นŠ์€ ๋ณต์‚ฌ์˜ ์›๋ฆฌ๋ฅผ ๊ทธ๋ฆผ์œผ๋กœ ์•Œ์•„๋ณด์ž.

์–•์€ ๋ณต์‚ฌ์™€ ๊นŠ์€ ๋ณต์‚ฌ์˜ ์ฐจ์ด๋Š” ์•ž์—์„œ ๋ฐฐ์› ๋˜ ๊ฐœ๋…์ธ ์ฐธ์กฐํ˜•์‹๊ณผ ๊ฐ’ํ˜•์‹์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ์‰ฝ๋‹ค. 

 

* ์–•์€ ๋ณต์‚ฌ: ์ฐธ์กฐ ํ˜•ํƒœ

 

* ๊นŠ์€ ๋ณต์‚ฌ: ๊ฐ’ ํ˜•ํƒœ

: ์–•์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด Heap ๋ฉ”๋ชจ๋ฆฌ์— ์žˆ๋Š” ๊ฐ™์€ ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค์ง€๋งŒ, ๊นŠ์€ ๋ณต์‚ฌ๋ฅผ ํ•˜๋ฉด Heap ๋ฉ”๋ชจ๋ฆฌ์˜ ์ƒˆ๋กœ์šด ์ฃผ์†Œ์— ๋ณ€์ˆ˜ ๊ณต๊ฐ„์„ ๋งŒ๋“œ๋ฏ€๋กœ ๊ฐ’์„ ๋ถ„๋ฆฌํ•ด์„œ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

22.08.08(์›”), 22.08.11(๋ชฉ)

[5์ฐจ์‹œ - this, this() ์ƒ์„ฑ์ž]

* this: ๊ฐ์ฒด๊ฐ€ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ž์‹ ์„ ์ง€์นญํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ. ๋‚˜ ์ž์‹ ~!

namespace ThisEx{
    class Student{
    	private string name;
        public void SetName(string name){
        	this.name = name;
        }
    }
    class Program{
    	static void Main(string[] args){
        
        }
    }
}

: SetName() ํ•จ์ˆ˜์—์„œ name = name; ์ด๋ ‡๊ฒŒ ์ ์œผ๋ฉด ์•ž์˜ name ๋ณ€์ˆ˜๊ฐ€ SetName ํ•จ์ˆ˜ ์ธ์ž์— ์žˆ๋Š” name ์ธ์ง€, Student ํด๋ž˜์Šค์˜ name ์ธ์ง€ ์•Œ ์ˆ˜๊ฐ€ ์—†๋‹ค. ์ด๋Ÿด ๋•Œ this ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ = ์™ผ์ชฝ์€ Student ํด๋ž˜์Šค์˜ name, = ์˜ค๋ฅธ์ชฝ์€ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ name ์ด๋ผ๊ณ  ๋ช…์‹œํ•ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

 

* this(): ์ƒ์„ฑ์ž. ๋ฉ”์†Œ๋“œ์™€ ๋น„์Šทํ•œ ๊ฒƒ์œผ๋กœ ์˜ค๋ฒ„๋กœ๋”ฉ์ด ๊ฐ€๋Šฅํ•จ.

namespace ThisEx2{
    class Demo{
    	public int a, b, c;
        
        //a, b, c ๋“ฑ ๋ณ€์ˆ˜๋ฅผ ์ค‘๋ณต์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ ์ฝ”๋“œ. ๋ณด๊ธฐ ์•„์ฃผ ๋ถˆํŽธ...
        public Demo(){
            this.a = 100;
        }
        public Demo(int b){
            this.a = 100;
            this.b = b;
        }
        public Demo(int b, int c){
            this.a = 100;
            this.b = b;
            this.c = c;
        }
    }
    
    class Program{
    	static void Main(string[] args){
            Demo demo = new Demo(200, 300);
            
            Console.WriteLine(demo.b); //200
            Console.WriteLine(demo.c); //300
        }
    }
}

์œ„์—์„œ ๋ณ€์ˆ˜๋ฅผ ์ค‘๋ณต์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ๋ถˆํŽธํ–ˆ๋˜ ์ฝ”๋“œ๋ฅผ this() ์ƒ์„ฑ์ž๋ฅผ ์ด์šฉํ•ด์„œ ์ƒ์†๋ฐ›๊ณ  ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ ์ฝ”๋“œ

namespace ThisEx2{
    class Demo{
    	public int a, b, c;
        
        //a, b, c ๋“ฑ ๋ณ€์ˆ˜๋ฅผ ์ค‘๋ณต์ ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ƒ์„ฑ์ž ์˜ค๋ฒ„๋กœ๋”ฉ ์ฝ”๋“œ. ๋ณด๊ธฐ ์•„์ฃผ ๋ถˆํŽธ...
        public Demo(){
            this.a = 100;
            Console.WriteLine("Demo()"); //Demo()
        }
        public Demo(int b): this(){
            //this.a = 100; ์ฃผ์˜!! ๋งค๊ฐœ๋ณ€์ˆ˜์— ์ ์€ ๋ณ€์ˆ˜๋งŒ ์ฒ˜๋ฆฌํ•ด์•ผ ํ•จ.
            this.b = b;
            Console.WriteLine($"{Demo({b})"); //Demo(200)
        }
        public Demo(int b, int c): this(b){
            //this.a = 100;
            //tihs.b = b;
            this.c = c;
            Console.WriteLine($"{Demo({b} {c})"); //Demo(200 300)
        }
    }
    
    class Program{
    	static void Main(string[] args){
            Demo demo = new Demo(200, 300);
            
            Console.WriteLine(demo.a); //100
            Console.WriteLine(demo.b); //200
            Console.WriteLine(demo.c); //300
        }
    }
}

 

22.08.23(ํ™”)

[6~7์ฐจ์‹œ - ์ ‘๊ทผ ์ œํ•œ์ž(Access Modifier)]

* ์ ‘๊ทผ ์ œํ•œ์ž/ํ•œ์ •์ž ์ข…๋ฅ˜

public, private, protected, internal, protected internal, private protected

1) public: ํด๋ž˜์Šค์˜ ๋‚ด๋ถ€ ๋˜๋Š” ์™ธ๋ถ€ ๋ชจ๋“  ๊ณณ์—์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ์ง€์ •์ž

2) private: ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋„๋ก ํ•˜๋Š” ์ง€์ •์ž. ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ์ง€์ •์ž.

                ์ƒ์† ๋ฐ›์€ ์ž์‹(ํŒŒ์ƒ) ํด๋ž˜์Šค์—์„œ๋„ ์ ‘๊ทผ ํ—ˆ์šฉ ์•ˆ๋จ.

3) protected: ํด๋ž˜์Šค ์™ธ๋ถ€์—์„œ๋Š” ์ ‘๊ทผํ•  ์ˆ˜ ์—†๋„๋ก ํ•˜๋Š” ์ง€์ •์ž. ํ•˜์ง€๋งŒ ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ๋Š” ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๋„๋ก ํ•˜๋Š” ์ง€์ •์ž.

4) internal: ๋™์ผ ์–ด์…ˆ๋ธ”๋ฆฌ์— ์žˆ๋Š” ์ฝ”๋“œ์—์„œ๋งŒ public์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์ •์ž.

                  ๋‹ค๋ฅธ ์–ด์…ˆ๋ธ”๋ฆฌ์— ์žˆ๋Š” ์ฝ”๋“œ์—์„œ๋Š” private๊ณผ ๊ฐ™์€ ์ˆ˜์ค€์˜ ์ ‘๊ทผ ์ˆ˜์ค€์„ ๊ฐ–๋Š”๋‹ค. ์ฆ‰ ์ ‘๊ทผ ๋ถˆ๊ฐ€.

                  * ์–ด์…ˆ๋ธ”๋ฆฌ: ํด๋ž˜์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ(.dll), ์‹คํ–‰ํŒŒ์ผ(.exe) ๋“ฑ์˜ ํŒŒ์ผ๋“ค

5) protected internal: ๋™์ผ ์–ด์…ˆ๋ธ”๋ฆฌ์— ์žˆ๋Š” ์ฝ”๋“œ์—์„œ๋งŒ protected๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ์ง€์ •์ž.

                                  ๋‹ค๋ฅธ ์–ด์…ˆ๋ธ”๋ฆฌ์— ์žˆ๋Š” ์ฝ”๋“œ์—์„œ๋Š” private๊ณผ ๊ฐ™์€ ์ ‘๊ทผ ์ˆ˜์ค€์„ ๊ฐ–๋Š”๋‹ค. ์ฆ‰ ์ ‘๊ทผ ์•ˆ๋จ.

6) private protected: ๋™์ผ ์–ด์…ˆ๋ธ”๋ฆฌ์— ์žˆ๋Š” ํด๋ž˜์Šค์—์„œ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ๋งŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅ

 

* ํ•œ์ •์ž๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ์ง€ ์•Š์„ ๋•Œ ๊ธฐ๋ณธ ์…‹ํŒ… ๊ฐ’์€ private์ด๋‹ค.

 

์˜ˆ์‹œ)

namespace AccessModifierEx{
    class Demo{
    	public Demo(){
            Demo.public_method();
            Demo.private_method();
            Demo.protected_method();
            Demo.internal_method();
            Demo.protected_internal();
        }
        
        //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(){}
        
        //private protected ์ ‘๊ทผ ์ œํ•œ์ž
        private protected static void private_protected(){
        	//7.2๋ฒ„์ „์œผ๋กœ ์—…๊ทธ๋ ˆ์ด๋“œํ•ด์•ผ private protected ์‚ฌ์šฉ ๊ฐ€๋Šฅ. 
        }
    }
    
    class Demo2 : Demo{
    	public Demo2(){
            Demo.public_method();
            Demo.protected_method();
            Demo.protected_internal();
            Demo.private_protected();
            Demo.internal_method();
        }
    }
    
    class Program{
    	static void Main(string[] args){
            Demo.public_method();
            Demo.internal_method();
            Demo.protected_internal();
        }
    }
}

 

22.08.23(ํ™”)

[8์ฐจ์‹œ - ์ƒ์†, 9์ฐจ์‹œ - base ํ‚ค์›Œ๋“œ์™€ sealed ํ•œ์ •์ž]

* ์ƒ์†: ๋ถ€๋ชจ ํด๋ž˜์Šค๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ๋ณ€์ˆ˜, ํ•จ์ˆ˜ ๋“ฑ์„ ์žฌ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ.

* ์šฉ์–ด: ์ƒ์† or ๊ธฐ๋ฐ˜(๋ถ€๋ชจ) ํด๋ž˜์Šค, ํŒŒ์ƒ(์ž์‹) ํด๋ž˜์Šค

namespace InheritEx{

    class Parent{
    	public Parent(){
        	Console.WriteLine("Parent() ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
        }
        ~Parent(){
        	Console.WriteLine("~Parent() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ");
        }
    }    
    
    class Child : Parent{
    	public Child(){
        	Console.WriteLine("Child() ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
        }
        ~Child(){
        	Console.WriteLine("~Child() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ");
        }
    }
    
    class Program{
    	static void Main(string[] args){
            Child child = new Child();
        }
    }
}

 

์œ„ ์ฝ”๋“œ ๊ฒฐ๊ณผ 

Parent() ์ƒ์„ฑ์ž ํ˜ธ์ถœ

Child() ์ƒ์„ฑ์ž ํ˜ธ์ถœ

~Child() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ

~Parent() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ

=> ๋ถ€๋ชจ๋จผ์ € ์ƒ์„ฑํ•˜๊ณ  ์ž์‹์„ ์ƒ์„ฑํ•œ๋‹ค. ๋˜ํ•œ, ์ž์‹์„ ๋จผ์ € ์ข…๋ฃŒํ•˜๊ณ  ๋ถ€๋ชจ๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค.

 

* ๊ธฐ๋ฐ˜(๋ถ€๋ชจ) ํด๋ž˜์Šค์˜ ์ธ์ž ์ƒ์„ฑ์ž์—๊ฒŒ ํŒŒ์ƒ(์ž์‹) ํด๋ž˜์Šค๊ฐ€ ์ธ์ž๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๋ฐฉ๋ฒ•

-> base ํ‚ค์›Œ๋“œ๋กœ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๊ณ  ๊ธฐ๋ฐ˜(๋ถ€๋ชจ) ํด๋ž˜์Šค์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.

-> sealed ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ƒ์†์„ ๋ด‰์ธํ•˜๊ฒŒ ๋˜์–ด ์ƒ์†์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋„๋ก ํด๋ž˜์Šค๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.

namespace InheritEx2{

   class Parent{
    	protected string name;
    
    	public Parent(string name){
            this.name = name;
            Console.WriteLine($"{this.name}.Parnet() ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
        }
        ~Parent(){
            Console.WriteLine($"{this.name}.~Parnet() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ");
        }
        public void ParentMethod(){
            Console.WriteLine($"{name}.ParentMethod()");
        }
    }
    
    class Child : Parent{
        public Child(string name) : base(name){
            Console.WriteLine($"{this.name}.Child() ์ƒ์„ฑ์ž ํ˜ธ์ถœ");
        }
        ~Child(){
            Console.WriteLine($"{this.name}.~Child() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ");
        }
        public void ChildMethod(){
            Console.WriteLine($"{name}.ChildMethod()");
        }
    }

	class Program{
    	static void Main(string[] args){
            Parent parentA = new Parent("parentA");
            parentA.ParentMethod();
            
            Child childA = new Child("childA");
            childA.ChildMethod();
        }
    }
}

 

์œ„ ์ฝ”๋“œ ๊ฒฐ๊ณผ

parentA.Parent() ์ƒ์„ฑ์ž ํ˜ธ์ถœ

parentA.ParentMethod()

childA.Parent() ์ƒ์„ฑ์ž ํ˜ธ์ถœ

childA.Child() ์ƒ์„ฑ์ž ํ˜ธ์ถœ

childA.ChildMethod()

childA.~Child() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ

childA.~Parent() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ

parentA.~Parent() ์ข…๋ฃŒ์ž ํ˜ธ์ถœ

 

 

22.09.15(๋ชฉ)

[10์ฐจ์‹œ - ์ƒ์†๊ด€๊ณ„์˜ ํด๋ž˜์Šค ํ˜•๋ณ€ํ™˜]

: Casting ์—ฐ์‚ฐ์ž -> () ๊ด„ํ˜ธ ์ด์šฉ.

 

//์ƒ์†๊ด€๊ณ„์˜ ํด๋ž˜์Šค ์‚ฌ์ด์—์„œ์˜ ํ˜•๋ณ€ํ™˜

namespace ClassCoversionEx{

    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();
            
            Programmer programmer = new Programmer();
            programmer.programming();
            programmer.showInfo(); //Human ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ ๊ฐ€๋Šฅ
            
            Designer designer = new Designer();
            designer.design();
            designer.showInfo(); //Human ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜ธ์ถœ ๊ฐ€๋Šฅ
        }
    }
}
namespace ClassCoversionEx{

    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();
            
            human = new Programmer();// Programmer๋ฅผ ์ƒ์†๋ฐ›์•—๊ธฐ ๋•Œ๋ฌธ์— Human๊ฐ์ฒด๊ฐ€ Programmer๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.
            human.showInfo();
            
            Programmer programmer = (Programmer)human;
            programmer.showInfo();
            programmer.programming();
            
            human = new Designer();
            human.showInfo();
            
            Designer designer = (Designer)human;
            designer.showInfo();
            designer.design();
        }
    }
}

 

22.12.02(๊ธˆ)

[11์ฐจ์‹œ - is ์—ฐ์‚ฐ์ž์™€ as ์—ฐ์‚ฐ์ž]

ํ˜•๋ณ€ํ™˜๊ณผ ๊ด€๋ จ๋œ ์—ฐ์‚ฐ์ž: is, as

 

* as: ํ˜•๋ณ€ํ™˜(์บ์Šคํ‹ฐ)๊ณผ ๊ฐ™์€ ์—ญํ• ์„ ํ•˜๋Š” ์—ฐ์‚ฐ์ž. ํ˜•๋ณ€ํ™˜์— ์‹คํŒจํ–ˆ์„ ๊ฒฝ์šฐ null ๊ฐ’์„ ๋ฆฌํ„ดํ•œ๋‹ค.

* is: ํ•ด๋‹น ๊ฐ์ฒด์˜ Type(ํ˜•)์ด ์ผ์น˜ํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ bool ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ์ž.

namespace AsIsEx{
    class Human{
    	public void ShowInfo(){
            Console.WriteLine("Human");
        }
    }
    
    classs Programmer : Human {
    	public void Programming(){
            Console.WriteLine("์ฝ”๋”ฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ");
        }
    }
    
    class Designer(){
    	public void Design(){
            Console.WriteLine("๋””์ž์ธํ•˜๋Š” ๋””์ž์ด๋„ˆ");
        }
    }
    
    class Program{
    	static void Main(string[] args){
            Human human = new Programmer();
            Programmer programmer;
            
            if(human is Programmer){
            	programmer = (Programmer)human;
                programmer.Programming();
            }
            
            Human human2 = new Designer();
            
            Designer designer = human2 as Designer;
            if(designer != nulll) designer.design();
            
            Designer designer2 = human as Designer;
            if(designer2 != null) desiginer2.design();
            else Console.WriteLine("designer2๋Š” Designer ํ˜•์ด ์•„๋‹ˆ๋‹ค!");
        }
    }
}

์ถœ๋ ฅ๊ฐ’:

์ฝ”๋”ฉํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋จธ

๋””์ž์ธํ•˜๋Š” ๋””์ž์ด๋„ˆ

designer2๋Š” Designerํ˜•์ด ์•„๋‹ˆ๋‹ค!

 

[12์ฐจ์‹œ - ์˜ค๋ฒ„๋ผ์ด๋”ฉ(virtual, override, new)]

* ์˜ค๋ฒ„๋ผ์ด๋“œ: ๋ฉ”์†Œ๋“œ ์žฌ์ •์˜

1) virtual: ๋ถ€๋ชจ(๋ฒ ์ด์Šค) ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์•ž์— ๋ถ™์ด๋Š” ์—ฐ์‚ฐ์ž. ์ž์‹(ํŒŒ์ƒ. derived) ํด๋ž˜์Šค์—์„œ ์žฌ์ •์˜ ๋  ์ˆ˜ ์žˆ๋‹ค.(์•ˆํ•ด๋„ ์ƒ๊ด€ ์—†์Œ)

2) override: ์ž์‹(ํŒŒ์ƒ) ํด๋ž˜์Šค ๋ฉ”์†Œ๋“œ ์•ž์— ๋ถ™๋Š” ์—ฐ์‚ฐ์ž. ๋ถ€๋ชจ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์žฌ์ •์˜ ํ•œ๋‹ค๋ผ๋Š” ์˜๋ฏธ.

                     ์žฌ์ •์˜ ํ•  ๋•Œ๋Š” ๋ถ€๋ชจ์˜ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„๊ณผ ๊ฐ™์•„์•ผ ํ•œ๋‹ค. ํ”„๋กœํ† ํƒ€์ž…๋„ ์ผ์น˜ํ•ด์•ผ ํ•จ!

namespace OverrideEx{
    class BaseClass {
    	public virtual void echo(){
            Console.WriteLine("๊ธฐ๋ฐ˜ ํด๋ž˜์Šค");
        }
    }
    
    class DerivedClass : BaseClass {
    	public override void echo(){
            Console.WriteLine("ํŒŒ์ƒ ํด๋ž˜์Šค");
        }
    }    
    
    class Program {
    	static void Main(string[] args){
            DerivedClass derivedClass = new DerivedClass();
            derivedClass.echo(); // "ํŒŒ์ƒ ํด๋ž˜์Šค" ์ถœ๋ ฅ!
        }
    }
}

* ์ฐธ๊ณ ) virtual, override ํ‚ค์›Œ๋“œ ์—†์ด ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์ง€๋งŒ. ๊ฒฝ๊ณ ์ฐฝ์ด ๋œจ๊ธฐ๋„ ํ•˜๊ณ . ์›์น™์„ ์ง€์ผœ์ฃผ๋Š”๊ฒŒ ์ข‹๋‹ค.

 

namespace OverrideEx{
    class BaseClass {
    	public virtual void echo(){
            Console.WriteLine("๊ธฐ๋ฐ˜ ํด๋ž˜์Šค");
        }
    }
    
    class DerivedClass : BaseClass {
    	public new void echo(){
            Console.WriteLine("ํŒŒ์ƒ ํด๋ž˜์Šค");
        }
    }    
    
    class Program {
    	static void Main(string[] args){
            DerivedClass derivedClass = new DerivedClass();
            derivedClass.echo(); // "๊ธฐ๋ฐ˜ ํด๋ž˜์Šค" ์ถœ๋ ฅ!
        }
    }
}

* ์ฐธ๊ณ ) ํŒŒ์ƒ ํด๋ž˜์Šค์—์„œ new ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ์ž์‹ ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ์ถ”๊ณ  ๋ถ€๋ชจ ํด๋ž˜์Šค์— ์žˆ๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ๊ณผ ๋‹ค๋ฅธ ๊ฐœ๋….

 

[13์ฐจ์‹œ - ๊ตฌ์กฐ์ฒด(struct), ํŠœํ”Œ]

C#์—์„œ๋Š” struct๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Value Type์„ ๋งŒ๋“ค๊ณ  class๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Reference Type์„ ๋งŒ๋“ ๋‹ค.

int, double, float, bool ํƒ€์ž…์„ ๊ธฐ๋ณธ(Primitive Type)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ, struct๋กœ ์ •์˜๋œ Value Type์ด๋‹ค.

 

Value Type์€ ์ƒ์†๋  ์ˆ˜ ์—†์œผ๋ฉฐ, ์ฃผ๋กœ ๊ฐ„๋‹จํ•œ ๋ฐ์ดํ„ฐ ๊ฐ’์„ ์ €์žฅํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

Reference Type์€ class๋ฅผ ์ •์˜ํ•ด์„œ ๋งŒ๋“ค๊ณ , ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๊ณ  ์ข€ ๋” ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ์™€ ๊ธฐ๋Šฅ๋“ค์„ ์ •์˜ํ•˜๋Š” ๊ณณ์— ๋งŽ์ด ์‚ฌ์šฉ๋œ๋‹ค.

 

* ๊ตฌ์กฐ์ฒด: struct๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•ด์„œ ์ •์˜ํ•œ๋‹ค. ํด๋ž˜์Šค์™€ ๊ฐ™์ด ๋ฉ”์†Œ๋“œ, ์†์„ฑ(ํ”„๋กœํผํ‹ฐ) ๋“ฑ ๊ฑฐ์˜ ๋น„์Šทํ•œ ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€๋งŒ, ์ƒ์†์„ ํ•  ์ˆ˜ ์—†๋‹ค. ํด๋ž˜์Šค์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ธํ„ฐํŽ˜์ด์Šค(interface) ๊ตฌํ˜„์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

namespace StructEx{

    //๊ตฌ์กฐ์ฒด ์ •์˜
    struct MyPoint{
        public int X;
        public int Y;
            
        //๊ตฌ์กฐ์ฒด๋Š” ์ƒ์„ฑ์ž๋ฅผ ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค. but ๊ธฐ๋ณธ ์ƒ์„ฑ์ž(๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€ ์—†๋Š” ์ƒ์„ฑ์ž)๋Š” ์ƒ์„ฑ ์•ˆ๋จ!
        public MyPoint(int x, int y){
            this.X = x;
            this.Y = y;
        }
        
        //๋ชจ๋“  ๊ตฌ์กฐ์ฒด๋Š” System.Object ํ˜•์‹์„ ์ƒ์†ํ•˜๋Š” System.ValueType์œผ๋กœ๋ถ€ํ„ฐ ์ง์ ‘ ์ƒ์†๋ฐ›์Œ
        public string ToString(){
            return string.Format($"{X}, {Y}");
        }        
    }
        
    class Program{
    	static void Main(string[] args){
            MyPoint myPoint;
            myPoint.X = 100;
            myPoint.Y = 100;
            
            Console.WriteLine(myPoint.ToString());//100, 100
            
            MyPoint myPoint2 = new MyPoint(100,1000);
            MyPoint myPoint3 = myPoint2;//myPoint2 ๋ณต์‚ฌ
            myPoint3.Y = 1001;
            
            Console.WriteLine(myPoint2.ToString());//1000, 1000
            Console.WriteLine(myPoint3.ToString());//1000, 1001
        }
    }
}

๊ตฌ์กฐ์ฒด๋Š” ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ณต์‚ฌํ•  ๋•Œ ๊นŠ์€ ๋ณต์‚ฌ๊ฐ€ ์ด๋ค„์ง„๋‹ค!

 

* ํŠœํ”Œ: ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐ’(Field)์„ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๊ตฌ์กฐ์ฒด์™€ ๊ฐ™์€ ๊ฒƒ. () ์•ˆ์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํ•„๋“œ๋ฅผ ์ง€์ •ํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

C# 7.0 ์ด์ „ ๋ฒ„์ „์—์„œ๋Š” ๋ฉ”์†Œ๋“œ์—์„œ ํ•˜๋‚˜์˜ ๊ฐ’๋งŒ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. C# 7.0 ์ดํ›„ ๋ฒ„์ „๋ถ€ํ„ฐ๋Š” ํŠœํ”„์„ ์‚ฌ์šฉํ•ด์„œ ๋ณต์ˆ˜๊ฐœ์˜ ๊ฐ’์„ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค. 

namespace TupleEx {
    //unnamed tuple ์„ ์–ธ
    var t = (100, 200);
    
    //named tuple ์„ ์–ธ
    var t = (Name: "ํ™๊ธธ๋™", Id: "120")
    
    class Program{
    	static void Main(string[] args){
            var aa = ("ํ™๊ธธ๋™", 40);
            Console.WriteLine($"{aa.Item1}, {aa.Item2}");//ํ™๊ธธ๋™, 40
            
            var bb = (Name:"ํ™๊ธธ๋ณต", Age:55);
            Console.WriteLine($"{bb.Name}, {bb.Age}");//ํ™๊ธธ๋ณต, 55
            
            var (name, age) = bb;
            Console.WriteLine($"{name}, {age}");//ํ™๊ธธ๋ณต, 55
            
            bb = aa; //๊นŠ์€๋ณต์‚ฌ
            Console.WriteLine($"{bb.Name}, {bb.Age}");//ํ™๊ธธ๋™, 40
        }
    }
}

 

ํŒจํ‚ค์ง€ ๊ด€๋ฆฌ์ž ์ฝ˜์†”์—์„œ ํŠœํ”Œ ํ•ด๋‹น ํŒจํ‚ค์ง€๋ฅผ ์„ค์น˜ํ•ด์ค˜์•ผ ํ•œ๋‹ค.

PM> Install -Package "System.ValueTuple"

 

 

[14์ฐจ์‹œ - ํŠœํ”Œ ๋ฆฌํ„ด ํƒ€์ž…์„ ์ด์šฉํ•œ ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉํ•˜๊ธฐ]

 

 

22.12.29(๋ชฉ)

[15์ฐจ์‹œ - ์ธํ„ฐํŽ˜์ด์Šค ์ดํ•ด]

* ์ธํ„ฐํŽ˜์ด์Šค

- ๋ฌผ๋ฆฌ์ ์ธ ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ฐœ๋…: ๋‹ค๋ฅธ ๋‘ ์‹œ์Šคํ…œ ์žฅ์น˜๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋Š” ์ค‘๊ฐ„ ์ ‘์† ์žฅ์น˜. ex) ํ‚ค๋ณด๋“œ, ๋งˆ์šฐ์Šค, ๋ชจ๋‹ˆํ„ฐ ๋“ฑ์ด ์ปดํ“จํ„ฐ์— ์—ฐ๊ฒฐ๋˜์–ด ์‚ฌ์šฉ์ž์™€ ์ปดํ“จํ„ฐ๊ฐ„์˜ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•œ๋‹ค.

- ์ฝ”๋“œ ์ƒ์—์„œ์˜ ์ธํ„ฐํŽ˜์ด์Šค ๊ฐœ๋…: interface ํ‚ค์›Œ๋“œ ์‚ฌ์šฉํ•ด์„œ ๋งŒ๋“  ์ผ์ข…์˜ ๋ช…์„ธ์„œ. ๋‘ ์†Œํ”„ํŠธ์›จ์–ด ๊ฐ„์˜ ์—ฐ๊ฒฐ์žฅ์น˜.

 

* ์ธํ„ฐํŽ˜์ด์Šค์˜ ํŠน์ง•

- ์ธํ„ฐํŽ˜์ด์Šค๋Š” ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋ฉ”์†Œ๋“œ, ์ด๋ฒคํŠธ, ํ”„๋กœํผํ‹ฐ๋งŒ์„ ๋ฉค๋ฒ„๋กœ ๊ฐ–๋Š”๋‹ค.
- ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋ชจ๋“  ๋ฉค๋ฒ„ ์ ‘๊ทผ์ง€์ •์ž๋Š” public ์ด๋‹ค. ๋”ฐ๋ผ์„œ, ์ ‘๊ทผ์ œํ•œ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.

    wyh? ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š”๋ฐ ์ ‘๊ทผ ์ง€์ •์ž๊ฐ€ ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋ฉด ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

- ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๊ตฌํ˜„๋ถ€(๋ชธํ†ต)๊ฐ€ ์—†๋Š” ์ถ”์ƒ๋ฉค๋ฒ„๋ฅผ ๊ฐ–๋Š”๋‹ค.

- ์ธํ„ฐํŽ˜์ด์Šค๋Š” ๋‹ค์ค‘ ์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

    => ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๋‹ค๋ฅธ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์† ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ํด๋ž˜์Šค, ๊ตฌ์กฐ์ฒด์—์„œ๋„ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

    => ์ž์‹ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค๋Š” ์ž์‹, ๋ถ€๋ชจ ํ•จ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ตฌํ˜„ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ ๋ฐฉ๋ฒ•)

interface ์ธํ„ฐํŽ˜์ด์Šค๋ช…
{
    ๋ฐ˜ํ™˜ํ˜•์‹ ๋ฉ”์†Œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜, ..);
    ...
}

 

์ฝ”๋“œ ์˜ˆ์‹œ)

namespace InterfaceEx{

    interface IMyInterfaceA
    {
    	void output();
    }
    
    interface IMyInterfaceB
    {
    	void output();
    }    
    
    class Program : IMyInterfaceA, IMyInterfaceB
    {
    	static void Main(string[] args)
        {
            MyClass myClass = new MyClass();
            
            IMyInterfaceA iA = myClass;
            iA.output();
            
            iMyInterfaceB iB = myClass;
            iB.output();
        }
        
        //์ธํ„ฐํŽ˜์ด์Šค ์ƒ์† ํ›„ ์„ ์–ธ๋ถ€ ํ•„์š”
        void iMyInterfaceA.output(){
            Console.WriteLine("A ์ธํ„ฐํŽ˜์ด์Šค output() ํ˜ธ์ถœ");
        }
        //์ธํ„ฐํŽ˜์ด์Šค ์ƒ์† ํ›„ ์„ ์–ธ๋ถ€ ํ•„์š”
        void iMyInterfaceB.output(){
            Console.WriteLine("B ์ธํ„ฐํŽ˜์ด์Šค output() ํ˜ธ์ถœ");
        }
    }
}

๊ฒฐ๊ณผ:

A ์ธํ„ฐํŽ˜์ด์Šค output() ํ˜ธ์ถœ

B ์ธํ„ฐํŽ˜์ด์Šค output() ํ˜ธ์ถœ

 

[16์ฐจ์‹œ - ์ถ”์ƒํด๋ž˜์Šค ์ดํ•ด]

* ์ถ”์ƒ ํด๋ž˜์Šค: ์ธํ„ฐํŽ˜์ด์Šค์™€ ๋น„์Šทํ•˜๋‹ค ํ•˜์ง€๋งŒ, ์ถ”์ƒํด๋ž˜์Šค๋Š” ๊ตฌํ˜„(๋ชธํ†ต)๋ถ€๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๊ณ  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€์งˆ ์ˆ˜๋Š” ์—†๋‹ค.

                       ์‚ฌ์šฉํ•˜๋Š” ํ•œ์ •์ž๋Š” abstract์™€ class ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

                       ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ํด๋ž˜์Šค์™€ ๋‹ฌ๋ฆฌ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ๊ฐ–๊ณ  ์žˆ๋‹ค.

                       ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ๋ฉค๋ฒ„์— ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ private ์„ค์ •๋œ๋‹ค.

* ์ถ”์ƒ ํด๋ž˜์Šค ์„ ์–ธํ˜•์‹

abstract class ํด๋ž˜์Šค ๋ช…
{
      //ํด๋ž˜์Šค์™€ ๋™์ผ
}

 

* ์ถ”์ƒ ๋ฉ”์†Œ๋“œ: ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋ฅผ ์ง€์ •ํ•  ๋•Œ abstract ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

                       ์ถ”์ƒ ๋ฉ”์†Œ๋“œ๋Š” private ์ด ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— public, protected, internal, protected internal ์ค‘์— ํ•˜๋‚˜๋กœ

                       ์ˆ˜์‹๋˜๋„๋ก ํ•˜๊ณ  ์žˆ๋‹ค.    

* ์ถ”์ƒ ๋ฉ”์†Œ๋“œ ์„ ์–ธํ˜•์‹

public abstract void A();

 

*์˜ˆ์‹œ

using System;

namespace Abstract
{
    abstract class MyAbstractClass
    {
        protected void ProtectedMethod()
        {
            Console.WriteLine("์ถ”์ƒ ํด๋ž˜์Šค์˜ PotectedMethod ํ˜ธ์ถœ");
        }
        public void PublicMethod()
        {
            Console.WriteLine("์ถ”์ƒ ํด๋ž˜์Šค์˜ PubicMethod ํ˜ธ์ถœ");
        }
        public abstract void AbstractMethod();
    }

    class Child : MyAbstractClass
    {
        public override void AbstractMethod()
        {
            Console.WriteLine("์ถ”์ƒ ํด๋ž˜์Šค์˜ AbstractMethod ํ˜ธ์ถœ");
            ProtectedMetod();
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            MyAbstractClass myAbstractClass = new MyAbstractClass();
            myAbstractClass.AbstractMethod();
            myAbstractClass.PublicMethod();
        }
    }
}

๊ตฌํ˜„ํ•ด์•ผํ•  ๋ถ€๋ถ„(abstract method)๊ณผ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•„๋„๋  ๋ถ€๋ถ„(abstract๊ฐ€ ์•„๋‹Œ method)์„ ์•Œ๋ ค์ค„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๊ตฌ์กฐ

 

[17์ฐจ์‹œ - ํ”„๋กœํผํ‹ฐ์˜ ์ดํ•ด]

* ํ”„๋กœํผํ‹ฐ: get, set ์ ‘๊ทผ์ž๋กœ ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ  ์ฝ์–ด์˜ค๋Š” ์†์„ฑ.

class EmployeeInfo
{
    public string Name
    {
        get
        {
            return Name;
        }
        set
        {
            Name = value;
        }
    }
}

 

- C# 7.0 ๋ถ€ํ„ฐ ์ž๋™ ํ”„๋กœํผํ‹ฐ ์ง€์›

class EmployeeInfo
{
    public string Name { get; set; }
}
class EmployeeInfo
{
    public string Name { get; set; } = "unknown"
}

์ดˆ๊ธฐ๊ฐ’ ์ง€์ •๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

class EmployeeInfo
{
    public string Name { get; private set; }
}

private set ์€ ๋‚ด๋ถ€์—์„œ๋งŒ ๊ฐ’์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๊ณ  ์™ธ๋ถ€์—์„  ์ฝ๊ธฐ๋งŒ ๊ฐ€๋Šฅํ•œ ๊ฐ’์ด๋ผ๋Š” ๋œป.

 

- ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ๊ฐ์ฒด์˜ ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•(์ƒ์„ฑ์ž์™€ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•)

namespace CshapTest
{
    class EmployeeInfo
    {
        public string Name { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            EmployeeInfo employee = new EmployeeInfo() {
                Name = "ํ™๊ธธ๋™"
            };
        }
    }
}

 

[21์ฐจ์‹œ - ArrayList ์‚ฌ์šฉํ•˜๊ธฐ]

* Collection(์ปฌ๋ ‰์…˜): ๋ฐ์ดํ„ฐ ๋ชจ์Œ์„ ๋‹ด๋Š” ์ž๋ฃŒ๊ตฌ์กฐ.

                                   ex) ArrayList, Queue, Stack, Hashtable

class Program
{
    static void Main(string[] args)
    {
        int[] array = new int[] { 1, 2, 3, 4 };
        Console.WriteLine(array.GetType());

        ArrayList arrayList = new ArrayList();
        arrayList.Add(1);
        arrayList.Add(2);
        arrayList.RemoveAt(1); // ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ์ฐพ์•„์„œ ์ œ๊ฑฐ
        arrayList.Insert(1, 1.2f); // Insert(index, data) // index ์œ„์น˜์— data ๋ฅผ ์‚ฝ์ž…
        
        foreach(object obj in arrayList)
        {
            Console.Write($"{obj} ");
        }
    }
}

Add ๋Š” ๋งจ ๋’ค๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ, Insert ๋Š” ์•ž์œผ๋กœ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ.

Collection ์˜ ๋ชจ๋“  ์š”์†Œ๋“ค์€ object ํƒ€์ž…์ด๊ธฐ ๋•Œ๋ฌธ์— foreach ๋ฌธ์—์„œ object obj  ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

 

[22์ฐจ์‹œ - Queue, Stack, Hashtable ์‚ฌ์šฉ]

* Queue: ์„ ์ž…์„ ์ถœ(FIFO) ์ž๋ฃŒ ๊ตฌ์กฐ.

                Enqueue(๋ฐ์ดํ„ฐ ์ž…๋ ฅ), Dequeue(๋ฐ์ดํ„ฐ ์ถœ๋ ฅ)

class Program
{
    static void Main(string[] args)
    {
        Queue q = new Queue();
        q.Enqueue(1);
        q.Enqueue(100);
        q.Enqueue(200);
        q.Dequeue();
        q.Enqueue("๊ฐ€๋‚˜๋‹ค");
        
        foreach(object obj in q)
        {
            Console.WriteLine(obj); 
        }
    }
}

100

200

๊ฐ€๋‚˜๋‹ค

 

* Stack: ํ›„์ž…์„ ์ถœ(LIFO) ์ž๋ฃŒ๊ตฌ์กฐ

               Push(๋ฐ์ดํ„ฐ ์ž…๋ ฅ), Pop(๋ฐ์ดํ„ฐ ์ถœ๋ ฅ)

class Program
{
    static void Main(string[] args)
    {
        Stack stack = new Stack();
        stack.Push(1);
        stack.Push(100);
        stack.Push(200);
        stack.Pop();
        stack.Push("๊ฐ€๋‚˜๋‹ค");

        foreach(object obj in stack)
        {
            Console.WriteLine(obj); 
        }
    }
}

๊ฐ€๋‚˜๋‹ค
100
1

 

* Hashtable: Hashing ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ด์šฉํ•œ ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์ด ์ด๋ค„์ง€๋Š” ๋ฐฉ์‹์˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜.

                     ํ‚ค๋ฅผ ์ด์šฉํ•ด์„œ ํ•œ๋ฒˆ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์–ด ์žˆ๋Š” ์ปฌ๋ ‰์…˜ ๋‚ด์˜ ์ฃผ์†Œ๋ฅผ ๊ณ„์‚ฐํ•ด ๋‚ธ๋‹ค.

class Program
{
    static void Main(string[] args)
    {
        Hashtable hashtable = new Hashtable();
        hashtable["Apple"] = "์‚ฌ๊ณผ";
        hashtable["Banana"] = "๋ฐ”๋‚˜๋‚˜";

        foreach(DictionaryEntry obj in hashtable)
        {
            Console.WriteLine($"{obj.Key} {obj.Value}"); 
        }
    }
}

Banana ๋ฐ”๋‚˜๋‚˜
Apple ์‚ฌ๊ณผ

 

[23์ฐจ์‹œ - ์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐํ™”, ์ธ๋ฑ์„œ]

class Program
{
    static void Main(string[] args)
    {
        //์ปฌ๋ ‰์…˜ ์ดˆ๊ธฐ์ž๋ฅผ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ• - stack, queue๋Š” ์ด์šฉํ•  ์ˆ˜ ์—†์Œ
        ArrayList arrayList1 = new ArrayList() { 10, 20, 30 };

        //๋ฐฐ์—ด์„ ์ด์šฉํ•œ ์ดˆ๊ธฐํ™” ๋ฐฉ๋ฒ•
        int[] array = { 11, 22, 33, 44 };
        ArrayList arrayList2 = new ArrayList(array);
        Stack stack = new Stack(array);
        Queue queue = new Queue(array);

        //Hashtable ์€ Dictionary Initialiser๋ฅผ ์ด์šฉํ•œ๋‹ค.
        Hashtable hashtable = new Hashtable() {
            ["Seoule"] = "์„œ์šธ", //{"Seoule", "์„œ์šธ"} ์ด๋Ÿฐ ํ˜•ํƒœ๋„ ๊ฐ€๋Šฅ
            ["Busan"] = "๋ถ€์‚ฐ", //{"Busan", "๋ถ€์‚ฐ"} ์ด๋Ÿฐ ํ˜•ํƒœ๋„ ๊ฐ€๋Šฅ
        };
    }
}

 

//์ธ๋ฑ์„œ

 

[24์ฐจ์‹œ - yield ํ‚ค์›Œ๋“œ]

* Enumerator(Iterator): ์ง‘ํ•ฉ์ ์ธ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ํ˜ธ์ถœ์ž์—๊ฒŒ ๋ณด๋‚ด์ฃผ๋Š” ๊ธฐ๋Šฅ. ๋ฐ˜๋ณต์ž.

                                        IEnumerator ๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

* yield ํ‚ค์›Œ๋“œ: ํ˜ธ์ถœ์ž์—๊ฒŒ ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฆฌํ„ดํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ํ‚ค์›Œ๋“œ

        - yield return: ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•˜๋‚˜์”ฉ ๋ฆฌํ„ดํ•˜๋Š”๋ฐ ์‚ฌ์šฉ

        - yield break: ๋ฆฌํ„ด์„ ์ค‘์ง€ํ•˜๊ณ  Iteration ๋ฃจํ”„๋ฅผ ๋น ์ ธ ๋‚˜์˜ฌ ๋•Œ ์‚ฌ์šฉ

 

์ปฌ๋ ‰์…˜ ํด๋ž˜์Šค๋Š” Enumeration ์ด ๊ฐ€๋Šฅํ•œ ํด๋ž˜์Šค ์ธ๋ฐ, ์ด๋Ÿฌํ•œ ํด๋ž˜์Šค๋“ค์„ Enumerable ํด๋ž˜์Šค๋ผ๊ณ  ํ•œ๋‹ค.

์ด Enumerable ํด๋ž˜์Šค๋Š” IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•œ๋‹ค.

 

IEnumerable ์ธํ„ฐํŽ˜์ด์Šค๋Š” GetEnumerator() ๋ฉ”์†Œ๋“œ ํ•˜๋‚˜๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

์ปฌ๋ ‰์…˜ ํƒ€์ž…์ด๋‚˜ Enumerable ํด๋ž˜์Šค์—์„œ GetEnumerator() ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ yield ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

GetEnumerator() ๋ฉ”์†Œ๋“œ์—์„œ yield return ์‚ฌ์šฉํ•˜๋ฉด ์ปฌ๋ ‰์…˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ˆœ์ฐจ์ ์œผ๋กœ ํ•˜๋‚˜์”ฉ ๋„˜๊ฒจ์ฃผ๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ  ๋ฆฌํ„ดํƒ€์ž…์€ IEnumerator ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ๋‹ค.

class MyList
{
    private int[] data = { 1, 2, 3, 4, 5 };
    public IEnumerator GetEnumerator()
    {
        int i = 0;
        while (i < data.Length)
        {
            yield return data[i];
            i++;
        }
    }
}

 

728x90