카테고리 없음

[C#] 기본 내용 코드

vhxpffltm 2020. 3. 16. 21:10

C# 역시 C/C++ 과 비슷하다.

 

자세한 내용을 적기보단 작성한 코드를 통해 다른 언어와 비슷함을 알 수 있다.

 

함수, 클래스, 배열 정도에 관한 내용이다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication3
{
    class C
    {
        private int num;
        public C(int num)
        {
            //num = num;
            this.num = num;
        }
        public void show()
        {
            Console.WriteLine("Num.... : " + num);
        }
    }
 
    class parent //sealed는 상속 못하게 함
    {
        public int n;
        public parent() { Console.WriteLine("부모 생성자...."); }
    }
 
    class child : parent
    {
        public child(int num)
        {
            this.n = num;
            //base.n = num; base에 커서하면 parent와 묶임... 즉 base는 parent
            Console.WriteLine("자식 생성자....");
        }
        public void show()
        {
            Console.WriteLine("num값은....." + n);
        }
    }
 
    class myname
    {
        private string name = "HUR";
        public string Name
        {
            get{ return name;  }
            set{ name = value; }
            //Name이란 이름으로 get/set 접근자를 통해 name에 접근할 수 있으며, 
            //get 영역 내에서는 name의 값을 반환하고, 
            //set 영역 내에서는 name 속성에 value 값으로 초기화시킵니다. 
            //여기서 value은 Name으로 넘어온 값이라고 생각
        }
    }
 
    class virtual1
    {
        public virtual void show() { Console.WriteLine("1번(virtual) 메서드 호출...."); }
    }
    class virtual2 : virtual1
    {
        public override void show() { Console.WriteLine("2번(ovveride) 메서드 호출...."); }
    }
    class virtual3 : virtual1
    {
        public override void show() { Console.WriteLine("3번(override) 메서드 호출....."); }
    }
    //virtual 키워드는 자식 클래스에서 메소드를 재정의 하고 싶을때 재정의 될 부모 클래스의 메소드에 사용되며, 
    //override 키워드는 부모 클래스 내에서 virtual로 선언된 메소드를 재정의 하겠다는 표시를 하는 것과 같다.
 
    class body
    {
        public string bs = "base";
        public body() { Console.WriteLine("Body 생성자....."); }
        public void what() { Console.WriteLine("Base...: " + bs); }
    }
    class face : body
    {
        public string fs = "face";
        public face() { Console.WriteLine("Face 생성자......."); }
        public void what() { Console.WriteLine("face....:" + fs); }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            //int a;
            Console.WriteLine("{0}", add(123));
            Console.WriteLine(add(12345));
            //메서드 사용법 및 params
 
            int[] arr = new int[6] { 123456 };
            int[] arr2 = { 123 };
            int[,] arr3 = { { 12 }, { 23 }, { 34 } };
            int[,] arr4 = new int[46];
            Console.WriteLine(arr3.GetLength(0));
            // 1차원 배열, 2차원 배열 사용법
            for(int i = 0; i < arr4.GetLength(0); i++)
            {
                for(int j = 0; j < arr4.GetLength(1); j++)
                {
                    arr4[i, j] = i + j;
                    Console.Write("{0} ", arr4[i,j]);
                }
                Console.WriteLine();
            } // 2차원 배열 출력 
 
            C c = new C(30);
            c.show();
            // 클래사 및 생성자 사용법
 
            child ch = new child(100);
            ch.show();
            // 상속 하는방법
 
            myname my = new myname();
            Console.WriteLine("my.name()......: " + my.Name);
            my.Name = "??????";
            Console.WriteLine("my.name()......: " + my.Name);
            //get set사용법
 
            virtual1 v1 = new virtual1();
            v1.show();
            virtual2 v2 = new virtual2();
            v2.show();
            virtual3 v3 = new virtual3();
            v3.show();
            //virtual override를 통한 재정의
 
 
            //face fa = new face();
            //body bo = fa; // 업캐스팅
            //face ce = (face)bo; // 다운캐스팅
 
            body b = new body();
            face f = new face();
            b = f;
            b.what(); //업 캐스팅: 왜 Base의 what이 되었는가? -> b는 body의 객체이미로 face 객체의 body가 가지고 있는 정보밖에 없으므로 body의 what을 선택한다.
 
            face ff = (face)b;
            ff.what();
            //업캐스팅이란 자식 클래스의 객체가 부모 클래스의 형태로 변환되는 것. 
            //다운캐스팅(downcasting)은 부모 클래스의 객체가 자식 클래스의 형태로 변환
            //다형성(제일 중요)
 
            Console.WriteLine("null check....:  " + NULL("dksjdj38"4));
            //null 체크방법: ?. , ?[] ?이후의 인덱스 혹은 변수를 체크
 
        }
        static int add(params int[] arr)
        {
            int s = 0;
            for(int i = 0; i < arr.Length; i++)
            {
                s = s + arr[i];
            }
            return s;
        }
 
        static string NULL(string value, int len)
        {
            return value?.Substring(0Math.Min(value.Length, len));
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

Reference

https://exynoa.tistory.com/category