C# Collection Interface Delegate

http://msdn.microsoft.com/en-us/library/system.collections.generic(v=VS.80).aspx

Collections (예로, List, ArrayList, Hashtable등)은 ICollection<T> 인터페이스를 구현한다.
List 클래스의 경우는 IList<T> 인터페이스를 또한 구현한다.

List<T> 컬랙션 클래스의 메소드 (예를 들어, Exists, Find, TrueForAll 등)는 특정 헬퍼대리자 (예를 들어, delegate void Action<T>, delegate bool Predicate<T> 등과 같은)를 사용하여, 중복되는 코드를 간결하게 만들수있다.

Collection, Interface, Delegate

1. Collection
서로 밀접하게 관련된 데이터를 그룹화하여 좀 더 효율적으로 처리할 수 있게 한 특수한 클래스 혹은 구조체이다.


2. Collection과 Interface
모든 닷넷 컬렉션은 ICollection<T> 인터페이스는 구현한다.
IList<T> 인터페이스는 인덱스를 사용하여 요소에 개별적으로 접근할 수 있는 컬렉션을 정의한다.
-T this [int index] { get; set; } //지정한 인덱스에 있는 요소를 가져오거나 설정
-int IndexOf(T item) //IList에서 특정 항목의 인덱스를 확인
-void Insert(int index, T item) //항목을 IList의 지정한 인덱스에 삽입
-void RemoveAt(int index) //지정한 인덱스에서 IList 항목을 제거


3. Collection과 Method와 Delegate
// Person 클래스
public class Person
{
private readonly string _name;


public string Name
{
get { return _name; }
}


private readonly int _age;


public int Age
{
get { return _age; }
}


public Person(string name, int age)
{
_name = name;
_age = age;
}


public override string ToString()
{
return string.Format(“이름 : {0}\t나이 : {1}”, _name, _age);
}
}


// List<T> 컬렉션에 Person 객체를 담는다
private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));
}


// aList에 포함된 세 사람이 모두 10세 이상인지를 확인한다 => 둘리와 고길동만 만족함
private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


bool result = true;
foreach (Person p in aList)
{
if (p.Age < 10)
{
result = false;
break;
}
}
}


// aList에 포함된 세 사람이 모두 이름이 두 글자인지를 확인한다 => 둘리만 만족함
private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


bool result = true;
foreach (Person p in aList)
{
if (p.Name.Length != 2)
{
result = false;
break;
}
}
}


// 위의 두 코드가 나이가 10세이상인지 또는 이름이 두 글자인지 조건을 만족하는지를 확인하는 부분이 반복
// List<T> 컬렉션의 TrueForAll 메소드는 조건을 만족하는 메소드가 존재한다면 true반환
public delegate bool Predicate<T>(T item);


public class List<T> : …
{


public bool TrueForAll(Predicate<T> match)
{
bool result = true;
foreach (T item in this)
{
if (match(item) == false)
{
result = false;
break;
}
}
}
}


private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


// 10살 이상인지를 검사
bool result1 = aList.TrueForAll(IsGreaterThanTen);


// 무명 대리자를 사용하여 이름이 두 글자인지 검사
bool result2 = aList.TrueForAll(delegate(Person p)
{ return p.Name.Length == 2; });
}


private static bool IsGreaterThanTen(Person p)
{
return p.Age >= 10;
}

4. List<T> 컬랙션 클래스의 메소드와 헬퍼 대리자


public List<TOutput> ConvertAll<TOutput> (Converter<T, TOutput> converter)
-리스트 객체의 각 원소를 TOutput 형으로 변환하여 리스트로 반환


public bool Exists(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 원소가 있는지 여부를 반환


public T Find(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 첫번째 원소를 반환


public List<T> FindAll(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 모든 원소를 리스트로 반환


public int FindIndex(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 첫번째 원소의 인덱스를 반환


public int FindLastIndex(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 마지막 원소의 인덱스를 반환


public void ForEach(Action<T> action)
-리스트에 있는 모든 원소에 대해 action을 수행


public bool TrueForAll(Predicate<T> match)
-리스트에 있는 모든 원소가 match 조건을 만족하는 지 여부를 반환


<<대리자(Delegate)>>


public delegate void Action<T>(T object)
-T 형의 매개변수를 하나 받고 반환값이 없는 메소드


public delegate TOutput Converter<TInput, TOutput>(TInput input)
-TInput 형의 매개변수를 받고 이를 TOutput 형으로 변환하여 반환하는 메소드


public delegate bool Predicate<T>(T object)
-T 형의 매개변수를 받아 그것이 특정 조건을 만족하는지를 반환하는 메소드


public delegate int Comparison<T>(T x, T y)
-x, y 두 객체를 비교하는 메소드로 x가 y보다 작으면 음수, 같으면 0, 크면 양수를 반환하는 메소드


5. List<T>의 Sort 메소드
public void Sort()
public void Sort(Comparison<T> comparison)
public void Sort(IComparer<T> comparer)
public void Sort(int index, int count, IComparer<T> comparer)


-public void Sort()
즉, 매개변수가 없는 Sort 메서드(void Sort())는 List<T>의 원소인 객체와 계약을 맺었는데,
그 계약의 내용은 List<T>의 원소인 객체는 int CompareTo(T other)와 같은 메서드를 가지고 있다는 것입니다.
따라서 실제 List<T>의 원소의 객체가 무엇이든지 간에 Sort 메서드는 그 객체의 CompareTo 메서드를 호출하면 정렬을 수행할 수 있다.
List<T>의 원소인 객체는 int CompareTo(T other)를 구현하여야 한다.
즉, Person이 int CompareTo(Person other) 메소드에 이름 순으로 구현하여야 계약이 성립한다.


public class Person : IComparable<Person>
{
….

// Sort
public int CompareTo(Person other)
{
return this.Name.CompareTo(other.Name);
}
}


private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


// 이름순으로 정렬
aList.Sort();
}

-public void Sort(Comparison<T> comparison)
이번에는 이름이 아니라 나이 순으로 정렬을 하는 경우,
int CompareTo(Person other) 내부 구현을 이름이 아니라 나이를 기준으로 정렬하도록 수정한다.
이 경우 동적으로 Sort를 다르게 할수없는 문제가 있으므로,
List<T>의 원소를 비교 가능한 객체라고 전제 하는 것이 아니라,
Sort 메서드의 매개 변수로 아예 List<T>의 원소들을 비교하는 메서드를 전달하는 것입니다.

public delegate int Comparison<T>(T x, T y)
Comparison 대리자의 의미는 x가 y보다 작으면 음수, 같으면 0, 크면 양수를 반환한다는 것이다.

public static int ComparePersonByName(Person x, Person y)
{
return x.Name.CompareTo(y.Name);
}

public static int ComparePersonByAge(Person x, Person y)
{
return x.Age.CompareTo(y.Age);
}


private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


// 이름순으로 정렬
aList.Sort(ComparePersonByName);
// 나이순으로 정렬
aList.Sort(ComparePersonByAge);
}

-public void Sort(IComparer<T> comparer)
Sort가 IComparer<T> 인터페이스를 매개변수로 받아 처리할 수 있다.
int Compare(T x, T y)

public class PersonNameComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Name.CompareTo(y.Name);
}
}


public class PersonAgeComparer : IComparer<Person>
{
public int Compare(Person x, Person y)
{
return x.Age.CompareTo(y.Age);
}
}


private static void Main(string[] args)
{
List<Person> aList = new List<Person>();
aList.Add(new Person(“둘리”, 1000));
aList.Add(new Person(“희동이”, 3));
aList.Add(new Person(“고길동”, 40));


// 이름순으로 정렬
aList.Sort(new PersonNameComparer());
// 나이순으로 정렬
aList.Sort(new PersonAgeComparer());


foreach(Person p in aList)
Console.WriteLine(p); // Person의 ToString 호출
}

Generic List Class

<<List<T> 컬랙션 클래스의 메소드>>

public List<TOutput> ConvertAll<TOutput> (Converter<T, TOutput> converter)
-리스트 객체의 각 원소를 TOutput 형으로 변환하여 리스트로 반환

public bool Exists(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 원소가 있는지 여부를 반환

public T Find(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 첫번째 원소를 반환

public List<T> FindAll(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 모든 원소를 리스트로 반환

public int FindIndex(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 첫번째 원소의 인덱스를 반환

public int FindLastIndex(Predicate<T> match)
-리스트에 있는 모든 원소 중 match 조건을 만족하는 마지막 원소의 인덱스를 반환

public void ForEach(Action<T> action)
-리스트에 있는 모든 원소에 대해 action을 수행

public bool TrueForAll(Predicate<T> match)
-리스트에 있는 모든 원소가 match 조건을 만족하는 지 여부를 반환

<<대리자(Delegate)>>

public delegate void Action<T>(T object)
-T 형의 매개변수를 하나 받고 반환값이 없는 메소드

public delegate TOutput Converter<TInput, TOutput>(TInput input)
-TInput 형의 매개변수를 받고 이를 TOutput 형으로 변환하여 반환하는 메소드

public delegate bool Predicate<T>(T object)
-T 형의 매개변수를 받아 그것이 특정 조건을 만족하는지를 반환하는 메소드

public delegate int Comparison<T>(T x, T y)
-x, y 두 객체를 비교하는 메소드로 x가 y보다 작으면 음수, 같으면 0, 크면 양수를 반환하는 메소드

ArrayList wicList

ArrayList wicList = new ArrayList();
do
{
/// 중간 생략
               WeatherIndex calc = WeatherIndexFactory.GetInstance(value);
                wicList.Add(calc);
/// 중간 생략
} while (Console.ReadKey().Key != ConsoleKey.Escape);

foreach (WeatherIndex wic in wicList)
{
/// 중간 생략
}

열지수 공식 계산

// F: 화씨온도 R: 상대습도
double HI = (-42.379 + (2.04901523 * F) + (10.14333127 * R) – (0.22475541 * F * R) – (0.00683770 * F * F) – (0.05481717 * R * R) + (0.00122874 * F * F * R) + (0.00085282 * F * R * R) – (0.00000199 * F * F * R * R));
HI = (HI – 32) / 1.8; // 화씨를 섭씨온도로 변환
HI = Math.Round(HI * 10) / 10.0;

Array: pass by value vs pass by reference

static void ChangeArray1(int[] arr) // copy of reference가 전달
{
arr[0] = 888; // 원본 배열의 첫번째 값은 888로 변경
for (int i = 0; i < arr.Length; i++ )
Console.WriteLine(“inside arr[{0}]={1}”, i, arr[i]);

arr = new int[5] { -3, -1, -2, -3, -4 }; // local 변수 재지정
for (int i = 0; i < arr.Length; i++ )
Console.WriteLine(“inside2 arr[{0}]={1}”, i, arr[i]);
}

static void ChangeArray2(ref int[] arr)
{
arr[0] = 888; // 원본 배열의 첫번째 값은 888로 변경
for (int i = 0; i < arr.Length; i++ )
Console.WriteLine(“inside arr[{0}]={1}”, i, arr[i]);

arr = new int[5] { -3, -1, -2, -3, -4 }; // 원본 배열이 변경
for (int i = 0; i < arr.Length; i++ )
Console.WriteLine(“inside2 arr[{0}]={1}”, i, arr[i]);
}

static void Main()
{
int[] myArray = { 1, 4, 5 };
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray1(myArray);
for (int j = 0; j < myArray.Length; j++ )
Console.WriteLine(“myArray[{0}]={1}”, j, myArray[j]);

myArray = new int[3] { 1, 4, 5 };
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray2(ref myArray);
for (int j = 0; j < myArray.Length; j++ )
Console.WriteLine(“myArray[{0}]={1}”, j, myArray[j]);
}



출력결과:
myArray[0]=1
inside arr[0]=888
inside arr[1]=4
inside
arr[2]=5
inside2 arr[0]=-3
inside2 arr[1]=-1
inside2 arr[2]=-2
inside2 arr[3]=-3
inside2
arr[4]=-4
myArray[0]=888
myArray[1]=4
myArray[2]=5

myArray[0]=1
inside
arr[0]=888
inside arr[1]=4
inside arr[2]=5
inside2 arr[0]=-3
inside2
arr[1]=-1
inside2 arr[2]=-2
inside2 arr[3]=-3
inside2
arr[4]=-4
myArray[0]=-3
myArray[1]=-1
myArray[2]=-2
myArray[3]=-3
myArray[4]=-4

Parameter Passing: pass by value vs pass by reference vs pass by output

http://dis.dankook.ac.kr/lectures/hci10/entry/pass-by-value와-pass-by-reference와-pass-by-output-비교

pass value type by value (값형식을 값에 의한 전달)



static void Square1(int x)
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square1(i);
Console.WriteLine(“i={0}”, i);
}

i=5
The value inside the method: 25
i=5

pass reference type by value (참조형식을 값에 의한 전달)
-copy of reference가 전달


static void ChangeArray1(int[] arr)
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // local 변수 재지정
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray1(myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}

myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=888

pass value type by reference (값형식을 참조에 의한 전달)
ref 키워드 사용


static void Square2(ref int x)
{
x *= x;
Console.WriteLine(“The value inside the method: {0}”, x);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
Square2(ref i);
Console.WriteLine(“i={0}”, i);
}

i=5
The value inside the method: 25
i=25

pass reference type by reference (참조형식을 참조에 의한 전달)
ref 키워드 사용


static void ChangeArray2(ref int[] arr)
{
arr[0]=888; // 원본 배열의 첫번째 값은 888로 변경
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 다시 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray2(ref myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}

myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=-3

pass value type by output (값형식을 output에 의한 전달)
out 키워드 사용


static void Square3(int x, out int result)
{
result = x*x;
Console.WriteLine(“The value inside the method: {0}”, result);
}
static void Main()
{
int i = 5;
Console.WriteLine(“i={0}”, i);
int result;
Square3(i, out result);
Console.WriteLine(“result={0}”, result);
}

i=5
The value inside the method: 25
result=25

pass reference type by output (참조형식을 output에 의한 전달)
out 키워드 사용


static void ChangeArray3(out int[] arr)
{
//arr[0]=888; // use of unassigned out parameter ‘arr’ ERROR
arr = new int[5] {-3, -1, -2, -3, -4}; // 원본 배열이 변경
Console.WriteLine(“The value inside the method: arr[0]={0}”, arr[0]);
}
static void Main()
{
int[] myArray = {1, 4, 5};
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
ChangeArray3(out myArray);
Console.WriteLine(“myArray[0]={0}”, myArray[0]);
}

myArray[0]=1
The value inside the method: arr[0]=-3
myArray[0]=-3

== operator

static void Main(string[] args)
{
// 값형식과 참조형식
int val1 = 5;
int val2 = 10;
if (val1 == val2) // false (val1과 val2는 값이 다름)
Console.WriteLine(“val1 == val2”);
else
Console.WriteLine(“val1 != val2”);
object val3 = val1;
object val4 = val1;
if (val3 == val4) // false (val3와 val4는 같은 곳을 참조하지 않음)
Console.WriteLine(“val3 == val4”);
else
Console.WriteLine(“val3 != val4”);

int val5 = (int)val3;
int val6 = (int)val4;
if (val5 == val6) // true (val5와 val6은 같은 값을 가짐)
Console.WriteLine(“val5 == val6”);
else
Console.WriteLine(“val5 != val6”);


Point pos1 = new Point(val1, val2);
Point pos2 = new Point(val1, val2);
if (pos1 == pos2) // false (만약 Point Class가 ==연산자와 != 연산자와 Equals와 GetHashCode를 구현하지 않았다면 같은 곳을 참조하고 있지 않음) true (만약 Point Class가 데이터의 내용이 같은지를 판별하는 ==연산자와 !=연산자와 Equals와 GetHashCode를 구현했다면)
Console.WriteLine(“pos1 == pos2”);
else
Console.WriteLine(“pos1 != pos2”);
if (pos1.X == pos2.X) // true
Console.WriteLine(“pos1.X == pos2.X”);
else
Console.WriteLine(“pos1.X != pos2.X”);
if (pos1.Y == pos2.Y) // true
Console.WriteLine(“pos1.Y == pos2.Y”);
else
Console.WriteLine(“pos1.Y != pos2.Y”);


Point pos3 = pos1;
Point pos4 = pos1;
if (pos3 == pos4) // true (pos3와 pos4는 같은 곳을 참조하고 있음)
Console.WriteLine(“pos3 == pos4”);
else
Console.WriteLine(“pos3 != pos4”);


object pos5 = pos3;
object pos6 = pos4;
if (pos5 == pos6) // true (pos5와 pos6는 같은 곳을 참조하고 있음)
Console.WriteLine(“pos5 == pos6”);
else
Console.WriteLine(“pos5 != pos6”);


Point pos7 = (Point)pos6;
if (pos1 == pos7) // true (pos1, pos3, pos4, pos6, pos7는 같은 곳을 참조하고 있음)
Console.WriteLine(“pos1 == pos7”);
else
Console.WriteLine(“pos1 != pos7”);
if (pos2 == pos7) // false (만약 Point Class가 ==연산자와 != 연산자와 Equals와 GetHashCode를 구현하지 않았다면 같은 곳을 참조하고 있지 않음) true (만약 Point Class가 데이터의 내용이 같은지를 판별하는 ==연산자와 !=연산자와 Equals와 GetHashCode를 구현했다면)
Console.WriteLine(“pos2 == pos7”);
else
Console.WriteLine(“pos2 != pos7”);

Just another Kyoung Shin Park’s Lectures Sites site