728x90
반응형
SMALL

■ VS 2005 (.NET 2.0, C# 2.0)의 주요 특징

1. 제네릭

# 과거 방식

ArrayList list = new ArrayList();

list.Add("Alice");

string name = (string)list[0];    # 형변환 필요, 문자형 숫자형이 동시에 들어갔을 땐, 런타임 오류 가능성 있음


# 제네릭 도입 후

List<string> names = new List<string> { "Alice", "Bob" };

string name2 = names[0];    # 형변환 불필요, 타입 안전

 

2. 익명 메서드

# 과거 방식

delegate void Print(string msg);    # 델리게이트 선언

 

# 반드시 메서드를 선언해야 한다.

private void MyPrint(string msg)

{

    MessageBox.Show(msg);

}

 

# Form에 Button1을 올려놓고, 클릭 이벤트에서 호출한다고 하면..

private void Button1_Click(object sender, EventArgs e)

{

    Print print1 = new Print(MyPrint);

    print1("Hello from C# 1.0");

}


# 익명 메서드 이후 - 메서드를 따로 선언한 필요 없이 한 번에 처리가 가능하다.

delegate void Print(string msg);    # 델리게이트 선언 (C# 1.0과 선언하는 방법은 동일함)

 

# Form에 Button2를 올려놓고, 클릭 이벤트에서 호출한다고 하면..

private void Button2_Click(object sender, EventArgs e)

{

    Print print2 = delegate(string msg)

    {

        MessageBox.Show(msg);

    };

    print2("Hello");

}

 

3. Nullable 값 형식

# 과거 방식

int age = 0;    # 0이나 -1로 'null'처럼 처리했음 (명확하지 않음)


# Nullable 이후 - ?를 사용하여 null도 대입이 가능하다.

int? age2 = null;

bool? isActive = true;

 

4. foreach 개선

# 과거 방식

string[] names = new string[] { "가", "나", "다" };

 

for (int i = 0; i < names.Count(); i++)

{

    MessageBox.Show(names[i]);

}


# 개선 후

string[] names = new string[] { "가", "나", "다" };

 

foreach (string n in names)

{

    MessageBox.Show(n);

}

 

5. 속성 접근자 분리

# 과거 방식

public int Age { get; set; }    # 모든 곳에서 set 가능


# 개선 후

public int Age { get; private set; }    # 외부에서 읽기만 가능 - get, set 따로 public, private을 설정할 수 있다.

 

6. static class 도입

# 과거 방식 - public static class라는 게 없었음, 그래서 아래와 같이 선언하여 사용

public class MathHelperOld

{

    private MathHelperOld()

    {

    }

 

    public static int Square(int x) => x * x;

}


# static class 이후

public static class MathHelper

{

    public static int Square(int x) => x * x;

}

 

7. partial class

# 과거 방식 - partial이라는 게 없었음


# 개선 후 - class 이름이 같더라도 앞에 partial을 쓰면 하나의 파일로 인식한다.

partial class Customer

{

    public string Name { get; set; }

}

 

partial class Customer

{

    public int Age { get; set; }

}

 

8. 이터레이터 (yield)

# 과거 방식 - IEnumerable 직접 구현, IEnumerator 구현 필요


# 개선 후

IEnumerable GetNumbers()

{

    yield return 1;

    yield return 2;

    yield return 3;

}

 

9. TryParse 패턴

# 과거 방식

try

{

    int value = int.Parse("123");

}

catch

{

    # 예외 처리

}


# 개선 후

if (int.TryParse("123", out int result))

{

    MessageBox.Show(result);

}

 

10. Nullable 관련 연산자

# 과거 방식

string user = null;

string displayName = (user != null) ? user : "(이름 없음)";


# 개선 후

string user = null;

string displayName = user ?? "(이름 없음)";

 

11. 명시적 인터페이스 구현 개선

# 과거 방식 - 인터페이스 충돌 시 불명확

interface IPrintable { void Print(); }

interface IExportable { void Print(); }

 

class Report : IPrintable, IExportable

{

    public void Print()    # 어떤 인터페이스의 Print인지 불명확

    {

        MessageBox.Show("불명확");

    }

}


# 개선 후 - 명시적 인터페이스 구현으로 명확화

class ReportImproved : IPrintable, IExportable

{

    void IPrintable.Print() => Console.WriteLine("Printable report");

    void IExportable.Print() => Console.WriteLine("Exportable report");

}

728x90
반응형
LIST

+ Recent posts