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”);

C# operator== vs Equals

http://msdn.microsoft.com/en-us/library/ms173147.aspx

// Operator Overloading
if (x == y) { // 컴파일시 x와 y가 동일한지 확인

// 참조형일 경우
object x = “hello”;
object y = ‘h’ + “ello”; // ensure it’s a different reference
if (x == y) { // 결과 => FALSE
if (x.Equals(y)) { // 결과 => TRUE

// string 형(참조형이긴 하지만)일 경우
// Equals와 equality operators (== and !=) 을 구현하여 string 객체의 값이 동일한지 확인
string x1 = “hello”;
string y1 = ‘h’ + “ello”; // ensure it’s a different reference
if (x1 == y1) { // 결과 => TRUE
if (x1.Equals(y1)) { // 결과 => TRUE

Interface

Interface (인터페이스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Interface

IEnumerable & IEnumerator Interface
-IEnumerable 인터페이스는 foreach를 사용하여 컬랙션을 반복하는 것을 지원하기 위해 구현하여 사용한다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IEnumerable
http://dis.dankook.ac.kr/lectures/hci09/entry/Enumerator

IEquatable Interface
-IEquatable 인터페이스는 두 객체간에 서로 내부 내용이 같은 지 (예: if(a == b))를 비교하기 위해 구현하여 사용한다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IEquatable
http://dis.dankook.ac.kr/lectures/hci10/72
http://dis.dankook.ac.kr/lectures/hci09/entry/Equals

IComparable Interface
-IComparable 인터페이스는 개체에 대한 기본 정렬(sort) 순서를 지정해주기 위해 구현하여 사용한다. 해당 개체를 배열이나 컬랙션에서 정렬하는데 필요하다.
http://dis.dankook.ac.kr/lectures/hci11/entry/IComparable

Method Overloading vs Method Overriding

http://dis.dankook.ac.kr/lectures/hci10/entry/Method-Overloading-vs-Method-Overriding

Method Overloading: 동일한 함수명에 매개변수가 다른 함수를 둘 이상 정의하는 것으로, 동일한 함수 기능을 수행하지만 다른 매개변수의 경우를 처리할 때 사용

Method Overriding: 상속받은 파생 클래스에서 동일한 함수명에 동일한 매개변수로 정의하여 함수를 재정의하는 것으로 상속되어진 함수의 기능을 변경해서 재사용하고 싶을 때 사용

class Point : IEquatable<Point>, IComparable<Point>
{
// static member field
protected static int count = 0;

// instance member field
protected int x, y;


// property
public int X
{
get { return this.x; }
set { this.x = value; }
}


public int Y
{
get { return this.y; }
set { this.y = value; }
}


public static int Count
{
get { return count; } // error: this.count 는 사용할 수 없음
}


// constructor & destructor
public Point() : this(0, 0) { }
public Point(int x, int y) { this.x = x; this.y = y; count++; }
~Point() { count–; }


// method
public void SetPosition(int x, int y) { this.x = x; this.y = y; }
public void Move(int x, int y) { this.x += x; this.y += y; }


// ToString method override
public override string ToString() { return (String.Format(“({0}, {1})”, x, y)); }
// virtual method
public virtual void Print() { Console.WriteLine(“X={0} Y={1}”, x, y); }

// static method
public static int GetCount() { return count; }

// operator++ overload
public static Point operator++(Point p) { ++p.x; ++p.y; return p; }
// operator== overload
public static bool operator==(Point p1, Point p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator!=(Point p1, Point p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y; }
public override bool Equals(object obj)
{
if (!(obj is Point))
return false;
return Equals((Point)obj);
}

// IEquatable
public bool Equals(Point other)
{
if (this.x == other.x && this.y == other.y)
return true;
return false;
}


// IComparable
public int CompareTo(Point other)
{
if (this.x > other.x)
{
return 1;
}
else if (this.x < other.x)
{
return -1;
}
else
{
return 0;
}
}
}




class Point3D : Point, IEquatable<Point3D>
{
// instance member field
protected int z;


// property
public int Z
{
get { return this.z; }
set { this.z = value; }
}


// constructor & destructor
public Point3D() : this(0, 0, 0) { }
public Point3D(int x, int y, int z) : base(x, y) { this.z = z; }
~Point3D() { }


public void SetPosition(int x, int y, int z) { base.SetPosition(x, y); this.z = z; }
public void Move(int x, int y, int z) { base.Move(x, y); this.z += z; }


// override ToString method
public override string ToString() { return (String.Format(“({0}, {1}, {2})”, x, y, z)); }
// overrided method
public override void Print() { Console.WriteLine(“X={0} Y={1} Z={2}”, x, y, z); }
// operator++ overload
public static Point3D operator++(Point3D p) { ++p.x; ++p.y; ++p.z; return p; }
// operator== overload
public static bool operator ==(Point3D p1, Point3D p2) { return p1.Equals(p2); }
// operator!= overload
public static bool operator !=(Point3D p1, Point3D p2) { return !p1.Equals(p2); }
public override int GetHashCode() { return x ^ y ^ z; }
public override bool Equals(object obj)
{
if (!(obj is Point3D))
return false;
return Equals((Point3D)obj);
}


// IEquatable
public bool Equals(Point3D other)
{
if (this.x == other.x && this.y == other.y && this.z == other.z)
return true;
return false;
}
}




class PointTest
{
// pass-by-value (value type)
static void Swap(int p1, int p2)
{
int p;
p = p1;
p1 = p2;
p2 = p;
}


// pass-by-reference (value type)
static void Swap(ref int p1, ref int p2)
{
int p;
p = p1;
p1 = p2;
p2 = p;
}


// pass-by-value (reference type)
static void Swap(int[] p1, int[] p2)
{
int[] p;
p = p1;
p1 = p2;
p2 = p;
}


// pass-by-value (reference type)
static void Swap(ref int[] p1, ref int[] p2)
{
int[] p;
p = p1;
p1 = p2;
p2 = p;
}


static void Swap(ref Point p1, ref Point p2)
{
Point p;
p = p1;
p1 = p2;
p2 = p;
}


static void Swap(ref Point3D p1, ref Point3D p2)
{
Point3D p;
p = p1;
p1 = p2;
p2 = p;
}


static void Main(string[] args)
{
// value type: pass-by-value/pass-by-reference
int i = 5, j = 10;
Console.WriteLine(“Before: i={0}, j={1}”, i, j);


Swap(i, j);
Console.WriteLine(“After integer pass-by-value swap: i={0}, j={1}”, i, j);


Swap(ref i, ref j);
Console.WriteLine(“After integer pass-by-reference Swap: i={0}, j={1}”, i, j);


// reference type: pass-by-value/pass-by-reference
int[] arr1 = { 1, 2, 3 };
int[] arr2 = { 4, 5, 6, 7, 8 };
Console.WriteLine(“Before array pass-by-value swap: “);
Console.Write(“arr1: “);
foreach (int a in arr1)
Console.Write(“{0} “, a);
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2)
Console.Write(“{0} “, a);
Console.WriteLine();


Swap(arr1, arr2);
Console.WriteLine(“After array pass-by-value swap: “);
Console.Write(“arr1: “);
foreach (int a in arr1)
Console.Write(“{0} “, a);
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2)
Console.Write(“{0} “, a);
Console.WriteLine();


Swap(ref arr1, ref arr2);
Console.WriteLine(“After array pass-by-reference swap: “);
Console.Write(“arr1: “);
foreach (int a in arr1)
Console.Write(“{0} “, a);
Console.WriteLine();
Console.Write(“arr2: “);
foreach (int a in arr2)
Console.Write(“{0} “, a);
Console.WriteLine();


Console.Write(“p1: “);
Point p1 = new Point();
Console.Write(“p2: “);
Point p2 = new Point();


// Point
p1.SetPosition(10, 10); // 변수 값 지정
Console.Write(“p1 SetPosition: “);
p1.Print(); // 현재 좌표 출력
p1.Move(20, 50); // 좌표 변경 (점의 위치 이동)
Console.Write(“p1 Move: “);
p1.Print(); // 현재 좌표 출력
Console.WriteLine(“p1=” + p1);


p2.SetPosition(20, 30); // 변수 값 지정
Console.Write(“p2 SetPosition: “);
p2.Print(); // 현재 좌표 출력


//포인터 객체 교환함수 호출
Console.WriteLine(“\nSwap”);
Swap(ref p1, ref p2);
Console.WriteLine(“p1: {0}”, p1);
Console.WriteLine(“p2: {0}”, p2);


Console.WriteLine();
Console.WriteLine(“p1.m_nX={0}, p1.m_nY={1}, Point.GetCount()={2}”, p1.X, p1.Y, Point.Count);
Console.WriteLine(“p2.m_nX={0}, p2.m_nY={1}, Point.GetCount()={2}”, p2.X, p2.Y, Point.Count);


// Point3D
Console.Write(“p3: “);
Point3D p3 = new Point3D();
Console.Write(“p4: “);
Point3D p4 = new Point3D();


Console.WriteLine();
p3.SetPosition(10, 20);
Console.Write(“p3 SetPosition: “);
p3.Print(); // 현재 좌표 출력
p3.SetPosition(30, 40, 50);
Console.Write(“p3 SetPosition: “);
p3.Print(); // 현재 좌표 출력
++p3;
Console.Write(“++p3: “);
p3.Print(); // 현재 좌표 출력
p3.Move(20, 30, 40); // 좌표 변경 (점의 위치 이동)
Console.Write(“p3 Move: “);
p3.Print(); // 현재 좌표 출력
p3.Move(10, 10); // 좌표 변경 (점의 위치 이동)
Console.Write(“p3 Move: “);
Console.WriteLine(p3); // p3는 Point클래스의 ToString이 호출


Console.WriteLine();
p4.SetPosition(100, 200, 300);
Console.Write(“p4 SetPosition: “);
p4.Print(); // 현재 좌표 출력
p4.Move(10, 10, 10); // 좌표 변경 (점의 위치 이동)
Console.Write(“p4 Move: “);
p4.Print(); // 현재 좌표 출력


//포인터 객체 교환함수 호출
Console.WriteLine(“\nSwap”);
Swap(ref p3, ref p4);
Console.WriteLine(“p1: {0}”, p3);
Console.WriteLine(“p2: {0}”, p4);


Console.WriteLine();
Console.WriteLine(“p3.m_nX={0}, p3.m_nY={1}, p3.m_nZ={2}, Point.GetCount()={3}”, p3.X, p3.Y, p3.Z, Point.GetCount());
Console.WriteLine(“p4.m_nX={0}, p4.m_nY={1}, p4.m_nZ={2}, Point.count={3}”, p4.X, p4.Y, p4.Z, Point.Count);


if (p3 == p4)
Console.WriteLine(“p3 == p4”);
if (p3 != p4)
Console.WriteLine(“p3 != p4”);


Console.Write(“p5: “);
Point p5 = p3;
p5.Print(); // 현재 좌표 출력 p5는 Point3D – polymorphism
Console.Write(“p6: “);
Point p6 = new Point3D(110, 210, 310); // p6도 Point3D
p6.Print(); // Point3D의 Print가 호출 (late binding) – polymorphism
Point3D p7 = (Point3D)p5;
Console.WriteLine(p7); // p7는 Point3D클래스의 ToString이 호출
if (p7 == p5)
Console.WriteLine(“p7 == p5”);
if (p7 == p6)
Console.WriteLine(“p7 == p6”);
}
}


 

class indexer, property, abstract, sealed

Indexer (인덱서)
http://dis.dankook.ac.kr/lectures/hci09/entry/Indexer

Property (속성)
http://dis.dankook.ac.kr/lectures/hci09/entry/Property

Abstract Class (추상 클래스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Abstract-class

Sealed Class (봉인 클래스)
http://dis.dankook.ac.kr/lectures/hci09/entry/Sealed-Class

polymorphism (다형성)
– Shape/Circle/Rectangle/Triangle/Square 클래스
– class 및 inheritance
– abstract class 및 polymorphism