C#

52.C# - GenericMethod (제네릭 메서드)

Godffs 2009. 8. 13. 19:13
반응형
제네릭메서드는 형식 매개변수 T를 사용, 여러개의 매개변수를 동시 처리 가능

 Program.cs
using System;

public class 제네릭메서드
{
    public static void Swap<T>(ref T a, ref T b) //T 제네릭으로  t자리 int
    {
        T  temp = a; a = b; b = temp;
    }
    public static void Main()
    {
        //[1] Input
        string[] data = { "a", "z", "d", "e", "c" };
        //[2] Process : Sort
        for (int i = 0; i < data.Length - 1; i++)
        {
            for (int j = i + 1; j < data.Length; j++)
            {
                if (String.Compare(data[i], data[j])>0)
                {
                    Swap<string>(ref data[i], ref data[j]);
                }
            }
        }
        //[3] Output
        for (int i = 0; i < data.Length; i++)
        {
            Console.WriteLine("{0}", data[i]); //3,4,5,8,9
        }
        Console.WriteLine();
    }
}

결과화면


52.GenericMethod.zip
다운로드

반응형