C# interface

    [C#] Collection과 Interface

    [C#] Collection과 Interface

    1. 핵심 정리 using System; using System.Collections.Generic; clss Program { static void Main() { List c1 = new List(); c1.Add(10); c1.Add(20); c1.Clear(); int n = c1[0]; // IList에 있는 메소드(인덱서) SortedSet c2 = new SortedSet(); c2.Add(10); c2.Add(20); c2.Clear(); int n2 = c2[0]; // error } } 위의 코드에서 List는 동적배열이고 SortedSet은 트리구조이다. 분명 내부적으로 데이터를 다루는 코드는 다를텐데 요소를 추가하는 메소드는 Add로 같다. 또한 모든 요소를 지우는 Clear메소드도..

    [C#] 인터페이스 (interface)

    [C#] 인터페이스 (interface)

    1. 핵심 정리 using System; class Stack { public void Clear() { } } class Queue { public void Clear() { } // public void clear() { } } class Program { static void Main() { } } 위와 같은 코드가 있다고 해보자. Stack 과 Queue 클래스가 있고 그 안에는 Clear라는 동일한 이름의 함수가 있다. 이렇게 된다면 가져다 쓰는 입장에서는 Stack이나 Queue나 Clear를 부르면 되기에 편하게 쓸 수 있다. 하지만 실수로 Queue클래스 내부에 clear로 대소문자를 틀린 함수를 선언했다고 생각해보자. 이렇게 되면 일관성도 없고 가져다 쓰는 입장에서도 혼란이 생길 수 있다..