Category Archives: C# Programming

C# Collection Interface Delegate

http://msdn.microsoft.com/en-us/library/system.collections.generic(v=VS.80).aspx
http://dis.dankook.ac.kr/lectures/hci10/entry/C-Collection-Interface-Delegate

http://dis.dankook.ac.kr/lectures/hci10/entry/Generic-List-Class

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

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

IComparable

class Person : IComparable<Person>
{
    public int Age { get; set; }
    public string Name { get; set; }

    public int CompareTo(Person other)
    {
          // Alphabetic sort if age is equal. [A to Z]
          if (this.Age== other.Age) {
                return this.Name.CompareTo(other.Name);
          }
          // Default to age sort. [High to low]
         return this.Age.CompareTo(other.Age);
    }

    public override string ToString()
    {
        // String representation.
        return this.Age.ToString() + “,” + this.Name;
    }
}

class Program
{
    static void Main()
    {
List<Person> list = new List<Person>();
list.Add(new Person() { Name = “Steve”, Age = 10 });
list.Add(new Person() { Name = “Janet”, Age = 11 });
list.Add(new Person() { Name = “Andrew”, Age = 10 });
list.Add(new Person() { Name = “Bill”, Age = 5 });
list.Add(new Person() { Name = “Lucy”, Age = 8 });

// Uses IComparable.CompareTo()
list.Sort();

// Uses Person.ToString
foreach (var element in list)
{
    Console.WriteLine(element);
}
    }
}

IEquatable

IEquatable 인터페이스는 두 객체간에 서로 내부 내용이 같은 지 (예: if(a == b))를 비교하기 위해 구현하여 사용한다.
   
    public class Point: IEquatable<Point>
    {
        private int x;
        private int y;


        public Point() : this(0, 0) { }


        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }


        public int X
        {
            get { return x; }
            set { x = value; }
        }


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


        public override int GetHashCode()
        {
            return x ^ y;
        }


        public override bool Equals(object obj)
        {
            if (!(obj is Point))
                return false;


            return Equals((Point)obj);
        }


        public bool Equals(Point other)
        {
            if (this.x != other.x)
                return false;
            return this.y == other.y;
        }


        public static bool operator ==(Point p1, Point p2)
        {
            return p1.Equals(p2);
        }


        public static bool operator !=(Point p1, Point p2)
        {
            return !p1.Equals(p2);
        }
    }

    class EqualsTest
    {
        static void Main(string[] args)
        {
            Point p1 = new Point();
            Point p2 = new Point();


            Point p3 = new Point(10, 20);
            Point p4 = new Point(30, 40);


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

IEnumerable

IEnumerable 인터페이스의 구현은 foreach를 사용하여 컬랙션을 반복하는 것을 지원하기 위해 사용한다.

public
class Person
{
    public Person(string fName, string lName)
    {
        this.firstName = fName;
        this.lastName = lName;
    }

    public string firstName;
    public string lastName;
}

public class People : IEnumerable
{
    private Person[] _people;
    public People(Person[] pArray)
    {
        _people = new Person[pArray.Length];

        for (int i = 0; i < pArray.Length; i++)
        {
            _people[i] = pArray[i];
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
       return (IEnumerator) GetEnumerator();
    }

    public PeopleEnum GetEnumerator()
    {
        return new PeopleEnum(_people);
    }
}

public class PeopleEnum : IEnumerator
{
    public Person[] _people;

    // Enumerators are positioned before the first element
    // until the first MoveNext() call.
    int position = -1;

    public PeopleEnum(Person[] list)
    {
        _people = list;
    }

    public bool MoveNext()
    {
        position++;
        return (position < _people.Length);
    }

    public void Reset()
    {
        position = -1;
    }

    object IEnumerator.Current
    {
        get
        {
            return Current;
        }
    }

    public Person Current
    {
        get
        {
            try
            {
                return _people[position];
            }
            catch (IndexOutOfRangeException)
            {
                throw new InvalidOperationException();
            }
        }
    }
}

class App
{
    static void Main()
    {
        Person[] peopleArray = new Person[3]
        {
            new Person(“John”, “Smith”),
            new Person(“Jim”, “Johnson”),
            new Person(“Sue”, “Rabon”),
        };

        People peopleList = new People(peopleArray);
        foreach (Person p in peopleList)
            Console.WriteLine(p.firstName + ” “ + p.lastName);   
    }
}

/* This code produces output similar to the following:
*
* John Smith
* Jim Johnson
* Sue Rabon
*
*/

http://msdn.microsoft.com/ko-kr/library/system.collections.ienumerable.aspx

Interface

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

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

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

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

Class & Inheritance

Indexer
http://dis.dankook.ac.kr/lectures/hci09/entry/Indexer

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

Method overloading (메소드 오버로딩) Method overriding (메소드 오버라이딩)
http://dis.dankook.ac.kr/lectures/hci09/entry/Method-Overloading

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

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

OOP

Class – defines the grouping of data and code, the “type” of an object
Instance – a specific allocation of a class
Message – sent to objects to make them act
Method – a “function” that an object knows how to perform
Instance Variables – a specific piece of data belonging to an object
Property – away to access instance variables and other attributes of an object
Encapsulation – keep implementation private and seperate from interface
Polymorphism – different objects, same interface
Inheritance – hierarchical organization. share code, customize or extend behaviors