... | @@ -107,22 +107,47 @@ public static void Main(string[] args) |
... | @@ -107,22 +107,47 @@ public static void Main(string[] args) |
|

|
|

|
|
|
|
|
|
### 정적 생성자
|
|
### 정적 생성자
|
|
정적 멤버를 초기화하는 기능을 한다. 클래스에 단 한 개만 존재 할 수 있다. 최초로 접근하는 시점에 단 한 번만 실행된다.
|
|
정적 생성자는 정적 데이터를 초기화하거나 한 번만 수행하면 되는 특정 작업을 수행하는 데 사용한다. 특징은 다음과 같다.
|
|
|
|
1. 접근 제한자와 매개변수를 갖지 않는다.
|
|
|
|
2. 첫 번째 인스턴스가 만들어지기 전이나 정적 멤버가 참조되기 전에 클래스를 초기화하기 위해 자동으로 호출된다.
|
|
|
|
3. 직접 호출할 수 없다.
|
|
|
|
4. 사용자는 프로그램에서 정적 생성자가 실행되는 시기를 제어할 수 없다.
|
|
|
|
|
|
```c#
|
|
```c#
|
|
public static Dog KingDog = new Dog("왕개"); //위 아래는 같은 코드가 된다.
|
|
class StaticConcTest
|
|
|
|
{
|
|
|
|
public static void Main()
|
|
|
|
{
|
|
|
|
Dog dog1 = new Dog(); //static, 일반 생성자 모두 호출
|
|
|
|
|
|
|
|
Dog dog2 = new Dog(); //일반 생성자만 호출
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static Dog KingDog = new Dog();
|
|
public class Dog
|
|
static Dog()
|
|
|
|
{
|
|
{
|
|
KingDog = new Dog("왕개");
|
|
static bool firstDogCreated = false;
|
|
}
|
|
|
|
|
|
static Dog()
|
|
|
|
{
|
|
|
|
Console.WriteLine("static 생성자");
|
|
|
|
firstDogCreated = true;
|
|
|
|
Console.WriteLine(firstDogCreated);
|
|
|
|
Console.WriteLine("첫 번째 개가 태어났습니다.");
|
|
|
|
}
|
|
|
|
|
|
|
|
public Dog()
|
|
|
|
{
|
|
|
|
Console.WriteLine("일반 생성자");
|
|
|
|
Console.WriteLine("개가 태어났습니다.");
|
|
|
|
}
|
|
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
<br />
|
|
<br />
|
|
|
|
|
|
## 네임스페이스 , using
|
|
## 네임스페이스 , using
|
|
자바의 패키지 개념과 같다. 이름이 같지만 소속도 다르고 실제 쓰임도 다른 클래스들의 이름 충돌을 막기 위해서 사용한다. 그리고 클래스들의 소속을 구분하는데 사용되는 것이 더 일반적이다.
|
|
자바의 패키지 개념과 유사하다. 패키지와 다른 점은 네임스페이스와 폴더가 꼭 대응되지 않아도 되는 것이다. 이름이 같지만 소속도 다르고 실제 쓰임도 다른 클래스들의 이름 충돌을 막기 위해서 사용한다. 그리고 클래스들의 소속을 구분하는데 사용되는 것이 더 일반적이다.
|
|
|
|
|
|
```c#
|
|
```c#
|
|
namespace NamespaceEx1
|
|
namespace NamespaceEx1
|
... | @@ -412,22 +437,22 @@ static void Main(string[] args) |
... | @@ -412,22 +437,22 @@ static void Main(string[] args) |
|
```
|
|
```
|
|
|
|
|
|
#### System.Array
|
|
#### System.Array
|
|
모든 배열은 Array 타입을 조상으로 둔다. 유용한 일부 프로퍼티와 메서드가 있다.
|
|
모든 배열은 Array 타입을 조상으로 둔다. 일부 프로퍼티와 메서드의 사용 예시다.
|
|
|
|
|
|
```c#
|
|
```c#
|
|
static void Main(string[] args)
|
|
static void Main(string[] args)
|
|
{
|
|
{
|
|
int[] intArray = new int[] { 2, 3, 1, 6, 4, 5, 2 };
|
|
int[] intArray = new int[] { 2, 3, 1, 6, 4, 5, 2 };
|
|
int i = 0;
|
|
int i = 0;
|
|
Console.WriteLine(intArray.Rank); //배열의 차수 1
|
|
Console.WriteLine(intArray.Rank); //Array.Rank(배열의 차수를 구한다.) 배열의 차수 1.
|
|
Console.WriteLine(intArray.Length); //배열의 길이
|
|
Console.WriteLine(intArray.Length); //Array.Length(배열의 길이를 구한다.) 배열의 길이 7.
|
|
|
|
|
|
foreach (int num in intArray)
|
|
foreach (int num in intArray)
|
|
{
|
|
{
|
|
Console.Write(" "+intArray.GetValue(i++)); //정렬 전 배열 출력
|
|
Console.Write(" "+intArray.GetValue(i++)); //Array.GetValue(i)(배열에서 i번째 값을 리턴한다.) //정렬 전 배열 출력
|
|
}
|
|
}
|
|
|
|
|
|
Array.Sort(intArray); //배열 정렬
|
|
Array.Sort(intArray); //Array.Sort(정렬 하려는 배열)(배열을 오름차순 정렬한다.) //배열 정렬
|
|
|
|
|
|
i = 0;
|
|
i = 0;
|
|
foreach (int num in intArray)
|
|
foreach (int num in intArray)
|
... | @@ -436,7 +461,7 @@ static void Main(string[] args) |
... | @@ -436,7 +461,7 @@ static void Main(string[] args) |
|
}
|
|
}
|
|
|
|
|
|
int[] copyArray = new int[intArray.Length];
|
|
int[] copyArray = new int[intArray.Length];
|
|
Array.Copy(intArray, copyArray, 5); //intArray에서 copyArray로 5개만 복사
|
|
Array.Copy(intArray, copyArray, 5); //Array.Copy(원본 배열, 복사 배열, 복사하려는 길이) //intArray에서 copyArray로 5개만 복사
|
|
|
|
|
|
i = 0;
|
|
i = 0;
|
|
foreach (int num in copyArray)
|
|
foreach (int num in copyArray)
|
... | @@ -775,7 +800,7 @@ class DeleTest |
... | @@ -775,7 +800,7 @@ class DeleTest |
|
CalcDelegate Plus = new CalcDelegate(plus);
|
|
CalcDelegate Plus = new CalcDelegate(plus);
|
|
CalcDelegate Minus = DeleTest.minus; //C# 2.0부터는 간단하게 사용가능.
|
|
CalcDelegate Minus = DeleTest.minus; //C# 2.0부터는 간단하게 사용가능.
|
|
CalcDelegate Multiply = new CalcDelegate(dt.multiply); //인스턴스 메서드
|
|
CalcDelegate Multiply = new CalcDelegate(dt.multiply); //인스턴스 메서드
|
|
CalcDelegate PlusAndMinus = Minus + Plus; //델리게이트 상대로 +,- 연산 가능.
|
|
CalcDelegate PlusAndMinus = Plus + Minus; //델리게이트 상대로 +,- 연산 가능.
|
|
|
|
|
|
Calc(3, 5, Plus);
|
|
Calc(3, 5, Plus);
|
|
Calc(7, 2, minus);
|
|
Calc(7, 2, minus);
|
... | @@ -1058,6 +1083,8 @@ class Program |
... | @@ -1058,6 +1083,8 @@ class Program |
|
{
|
|
{
|
|
Vector vec = new Vector(); //속성값 0으로 초기화.
|
|
Vector vec = new Vector(); //속성값 0으로 초기화.
|
|
Vector vec2; //이것도 선언 가능.
|
|
Vector vec2; //이것도 선언 가능.
|
|
|
|
|
|
|
|
var v = new Vector() { x = 1, y = 2 }; //이런식으로 간단히 값을 저장해두고 쓴다.
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
... | | ... | |