using System;
public class 제네릭메서드
{
public static void Swap(ref int a, ref int b)
{
int temp = a;
a = b;
b = temp;
}
public static void Main()
{
//input
int[] data = { 5, 4, 9, 3, 8 };
//int temp=0;
//process
for (int i = 0; i <= data.Length - 1; i++)
{
for (int j = 0; j < data.Length; j++)
{
if (data[i] < data[j])
{
//temp = data[i];
//data[i] = data[j];
//data[j] = temp;
Swap(ref data[i], ref data[j]);
}
}
}
//output
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine("{0}", data[i]);
}
}
}
//=========제네릭 메서드 : 형식 매개변수 T를 사용, 여러개의 매개변수 동시 처리
using System;
using System.Collections.Generic;
public class 제네릭메서드
{
private static void Swap<T>(ref T a, ref T b)
{
T temp = a;
a = b;
b = temp;
}
public static void Main()
{
//input
string[] data = { "a","z","c","b","f" };
//int temp=0;
//process
for (int i = 0; i <= data.Length - 1; i++)
{
for (int j = 0; j < data.Length; j++)
{
if (string.Compare(data[i], data[j]) < 0 )
{
//temp = data[i];
//data[i] = data[j];
//data[j] = temp;
Swap<string> (ref data[i], ref data[j]);
}
}
}
//output
for (int i = 0; i < data.Length; i++)
{
Console.WriteLine("{0}", data[i]);
}
}
}
'.Net Project > .Net 3.5 Sp1' 카테고리의 다른 글
69장 JavaScript Event (0) | 2009.08.13 |
---|---|
68장 익명(Anymous) (0) | 2009.08.13 |
66장 이벤트(Event) (0) | 2009.08.13 |
65장 델리게이트(delegate) (0) | 2009.08.13 |
64장 알고리즘(병합) (0) | 2009.08.12 |