Generic List Class Sort Method

public void Sort(Comparison<T> comparison)
http://msdn.microsoft.com/en-us/library/w56d4y5z.aspx

List<Person> pList = new List<Person>();
pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기

[…]
public class Person : IComparable<Person>
{
public static Comparison<Person> ComparePersonByID =
delegate(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
};

[…]
}

pList.Sort(ComparePersonByID); // pList를 ID로 정렬하기 (Comparison delegate)

public class Person : IComparable<Person>
{
public static Comparison<Person> ComparePersonByID =
delegate(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
};

[…]
}

pList.Sort(new PersonIDComparer()); // pList를 ID로 정렬하기 (IComparer)

public class PersonIDComparer : IComparer<Person>
{
public int Compare(Person p1, Person p2)
{
return p1.ID.CompareTo(p2.ID);
}
}

pList.Sort((p, q) => p.ID.CompareTo(q.ID)); // pList를 ID로 정렬하기 (lambda)

 

 

Array.ConvertAll

https://msdn.microsoft.com/ko-kr/library/kt456a2y(v=vs.110).aspx

public delegate TOutput Converter<in TInput, out TOutput>(
TInput input
)

 static System.Drawing.Point Point2DrawingPoint(Point p)
{
     return (System.Drawing.Point)p;
}

static void Main(string[] args)
{

List<Point> Vertices = new List<Point>()
{
new Point() { X = 400, Y = 10 },
new Point() { X = 300, Y = 210 },
new Point() { X = 500, Y = 210 }
};

// 1. Lambda expression
System.Drawing.Point[] points1 = Array.ConvertAll(Vertices.ToArray(), x => (System.Drawing.Point)x); // Use with Point.cs type conversion operator overload

// 2. Anonymous method
System.Drawing.Point[] points2 = Array.ConvertAll(Vertices.ToArray(), delegate(Point x) { return (System.Drawing.Point)x; });

// 3. Delegate (Converter<TInput, TOutput>)
System.Drawing.Point[] points3 = Array.ConvertAll(Vertices.ToArray(), new Converter<Point, System.Drawing.Point>(Point2DrawingPoint));

foreach (var v in Vertices)
Console.WriteLine(v);
foreach (var p in points1)
Console.WriteLine(p);
foreach (var p in points2)
Console.WriteLine(p);
foreach (var p in points3)
Console.WriteLine(p);

}