... | @@ -394,8 +394,95 @@ static void Main(string[] args) |
... | @@ -394,8 +394,95 @@ static void Main(string[] args) |
|
Animal animal2 = new Animal();
|
|
Animal animal2 = new Animal();
|
|
Animal animal3 = animal1;
|
|
Animal animal3 = animal1;
|
|
|
|
|
|
Console.WriteLine(animal1.Equals(animal2)); //서로 힙 메모리의 위치가 다르기 때문에 False.
|
|
Console.WriteLine(animal1.Equals(animal2)); //서로 가리키는 힙 메모리의 위치가 다르기 때문에 False.
|
|
Console.WriteLine(animal1.Equals(animal3)); //서로 같은 메모리를 가리키고 있기 떄문에 True.
|
|
Console.WriteLine(animal1.Equals(animal3)); //서로 같은 위치를 가리키고 있기 떄문에 True.
|
|
}
|
|
}
|
|
```
|
|
```
|
|
|
|
|
|
|
|
#### GetHashCode
|
|
|
|
특정 인스턴스를 고유하게 식별할 수 있는 4바이트 int 값을 반환한다. Equals의 참/거짓 판단은 이 GetHashCode값을 기준으로 이루어진다.
|
|
|
|
|
|
|
|
```c#
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
short num1 = 256;
|
|
|
|
int num2 = 256;
|
|
|
|
short num3 = 256;
|
|
|
|
|
|
|
|
Console.WriteLine(num1.GetHashCode()); //num1과 num3는 같은 값을 가리키므로 HashCode값도 일치한다.
|
|
|
|
Console.WriteLine(num3.GetHashCode());
|
|
|
|
Console.WriteLine(num2.GetHashCode()); //int 형은 HashCode와 범위값이 일치하므로 그대로 반환하게 설정됨.
|
|
|
|
|
|
|
|
Animal animal1 = new Animal();
|
|
|
|
Animal animal2 = new Animal();
|
|
|
|
|
|
|
|
Console.WriteLine(animal1.GetHashCode()); //힙 메모리 내의 다른 주소를 참조하므로 서로 다른 값을 반환한다.
|
|
|
|
Console.WriteLine(animal2.GetHashCode());
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
#### System.Array
|
|
|
|
모든 배열은 Array 타입을 조상으로 둔다. 유용한 일부 프로퍼티와 메서드가 있다.
|
|
|
|
|
|
|
|
```c#
|
|
|
|
static void Main(string[] args)
|
|
|
|
{
|
|
|
|
int[] intArray = new int[] { 2, 3, 1, 6, 4, 5, 2 };
|
|
|
|
int i = 0;
|
|
|
|
Console.WriteLine(intArray.Rank); //배열의 차수 1
|
|
|
|
Console.WriteLine(intArray.Length); //배열의 길이
|
|
|
|
|
|
|
|
foreach (int num in intArray)
|
|
|
|
{
|
|
|
|
Console.Write(" "+intArray.GetValue(i++)); //정렬 전 배열 출력
|
|
|
|
}
|
|
|
|
|
|
|
|
Array.Sort(intArray); //배열 정렬
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
foreach (int num in intArray)
|
|
|
|
{
|
|
|
|
Console.Write(" " + intArray.GetValue(i++)); //정렬 후 배열 출력
|
|
|
|
}
|
|
|
|
|
|
|
|
int[] copyArray = new int[intArray.Length];
|
|
|
|
Array.Copy(intArray, copyArray, 5); //intArray에서 copyArray로 5개만 복사
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
foreach (int num in copyArray)
|
|
|
|
{
|
|
|
|
Console.Write(" " + copyArray.GetValue(i++)); //복사 후 배열 출력
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
#### this
|
|
|
|
```c#
|
|
|
|
class Book
|
|
|
|
{
|
|
|
|
string title;
|
|
|
|
decimal isbn;
|
|
|
|
string author;
|
|
|
|
|
|
|
|
public Book(string title) : this(title, 0) { } //this 예약어를 이용해 생성자 내에서 다른 생성자 호출
|
|
|
|
|
|
|
|
public Book(string title, decimal isbn) : this(title, isbn, string.Empty) { }
|
|
|
|
|
|
|
|
public Book() : this(string.Empty, 0, string.Empty) { }
|
|
|
|
|
|
|
|
public Book(string title, decimal isbn, string author)
|
|
|
|
{
|
|
|
|
this.title = title;
|
|
|
|
this.isbn = isbn;
|
|
|
|
this.author = author;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
정적 멤버에선 this 예약어를 사용할 수 없다. this는 new로 할당된 '객체'를 가리키는 내부 식별자이기 때문이다.
|
|
|
|
|
|
|
|
#### base
|
|
|
|
자바의 super 예약어와 같다. this와 용법은 같지만 base는 가리키는 대상이 부모 클래스이다.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|