728x90
์ผ๋ฐํ ๋ฉ์๋
๋ฐ์ดํฐ ํ์ ์ ๋งค๊ฐ๋ณ์์ฒ๋ผ ๋ฐ์์ ์ธ ์ ์๋๋ก ํด์ค๋ค.
namespace GenericMeThodExample
{
class MainApp
{
static void CopyArray<T>(T[] source, T[] target)
{
for (int i = 0; i < source.Length; i++)
{
target[i] = source[i];
}
}
public static void Main(string[] args)
{
// int ํ์ ๋ฐฐ์ด์ ๋ฃ์ผ๋ฉด
int[] source = { 1, 2, 3, 4, 5 };
int[] target = new int[source.Length];
CopyArray(source, target); // ์๋์ผ๋ก intํ์ผ๋ก ์ธ์
Console.WriteLine(string.Join(", ", target));
// string ํ์ ๋ฐฐ์ด์ ๋ฃ์ผ๋ฉด
string[] source2 = { "a", "b", "c", "d", "e" };
string[] target2 = new string[source.Length];
CopyArray(source2, target2); // ์๋์ผ๋ก stringํ์ผ๋ก ์ธ์
Console.WriteLine(string.Join(", ", target2));
}
}
}
์ผ๋ฐํ ํด๋์ค
ํด๋์ค๋ ๋ฐ์ดํฐ ํ์๋ง ๋ค๋ฅด๊ฒ ํ๊ณ ์ถ์ ๊ฒฝ์ฐ, ๋ฐ์ดํฐ ํ์์ ๋งค๊ฐ๋ณ์์ฒ๋ผ ๋๊ณ ๋๋จธ์ง๋ ์ผ๋ฐํํ ์ ์๋ค.
namespace GenericClassExample
{
class MyList<T>
{
private T[] array;
// MyList ์ ์ธ ์ ํฌ๊ธฐ๊ฐ 3์ธ ๋ฐฐ์ด์ด ์์ฑ๋จ
public MyList()
{
array = new T[3];
}
// MyList ์ธ๋ฑ์
public T this[int index]
{
get
{
return array[index];
}
set
{
if (index >= array.Length)
{
Array.Resize(ref array, index + 1);
}
array[index] = value;
}
}
// Length ํจ์๋ฅผ ์ฐ๋ฉด ํ์ฌ ๊ธธ์ด ๋ฐํ
public int Length
{
get { return array.Length; }
}
// ํ์ฌ List ๋ชฉ๋ก์ ๋ํ๋ด๋ ํจ์
public string CurrentList
{
get
{
return string.Join(", ", array);
}
}
}
class MainApp
{
public static void Main(string[] args)
{
// string ๋ฐฐ์ด๋ก ์ ์ธ
MyList<string> list = new MyList<string>();
list[0] = "a";
list[1] = "b";
list[2] = "c";
list[3] = "d";
Console.WriteLine(list.Length);
Console.WriteLine(list.CurrentList);
// int ๋ฐฐ์ด๋ก ์ ์ธ
MyList<int> list2 = new MyList<int>();
list2[0] = 1;
list2[1] = 2;
list2[2] = 3;
list2[3] = 4;
Console.WriteLine(list2.Length);
Console.WriteLine(list2.CurrentList);
}
}
}
๋งค๊ฐ๋ณ์์ ์ ์ฝ์กฐ๊ฑด ์ถ๊ฐ
- Class Container<T> where T : struct => T๋ ๋ฐ๋์ ๊ฐ ํ์์ด์ด์ผ ํ๋ค.
public class Container<T> where T : struct { ... }
public class Program
{
public static void Main(string[] args)
{
Container<int double char struct enum ๋ฑ ๊ฐ ํ์> container = new Container<๊ฐํ์>(๊ฐ ํ์
);
}
}
- Class Container<T> where T : class => T๋ ๋ฐ๋์ ์ฐธ์กฐ ํ์์ด์ด์ผ ํ๋ค.
public class Container<T> where T : struct { ... }
public class Program
{
public static void Main(string[] args)
{
Container<string object class interface ๋ฑ์ ์ฐธ์กฐํ์> container = new Container<์ฐธ์กฐํ์>(์ฐธ์กฐ ํ์
);
}
}
- Class Container<T> where T : new() => T๋ ๋ฐ๋์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์๊ฐ ์์ด์ผ ํ๋ค.
public class Container<T> where T : new()
{
private T item;
public Container()
{
item = new T();
}
}
public class Program
{
public static void Main(string[] args)
{
Container<๋ฐ์ดํฐ ํ์
์๋ฌด๊ฑฐ๋> container = new Container<๋ฐ์ดํฐ ํ์
์๋ฌด๊ฑฐ๋>();
}
}
- Class Container<T> where T : ๊ธฐ๋ฐํด๋์ค๋ช => T๋ ๋ฐ๋์ ๋ช ์ํ ๊ธฐ๋ฐ ํด๋์ค์ ํ์ ํด๋์ค์ฌ์ผ ํ๋ค.
public class Container<T> where T : SomeBaseClass { ... }
public class Program
{
public static void Main(string[] args)
{
Container<SomeBaseClass ํน์ SomeBaseClass๋ฅผ ์์๋ฐ๋ ํด๋์ค> container = new Container<SomeBaseClass ํน์ SomeBaseClass๋ฅผ ์์๋ฐ๋ ํด๋์ค>(new SomeBaseClass ํน์ SomeBaseClass๋ฅผ ์์๋ฐ๋ ํด๋์ค());
}
}
- Class Container<T> where T : ๊ธฐ๋ฐ์ธํฐํ์ด์ค๋ช => T๋ ๋ฐ๋์ ๋ช ์ํ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํด์ผํ๋ค.
public class Container<T> where T : ISomeInterface { ... }
public class Program
{
public static void Main(string[] args)
{
Container< ISomeInterface> container = new Container< ISomeInterface>( ISomeInterface๋ฅผ ์์๋ฐ๋ ํด๋์ค());
}
}
- Class Container<T, U> where T : U => T๋ ๋ฐ๋์ ๋ ๋ค๋ฅธ ํ์ ๋งค๊ฐ๋ณ์ U๋ก๋ถํฐ ์์๋ฐ์ ํด๋์ค์ฌ์ผ ํ๋ค.
public class Container<T, U> where T : U { ... }
public class A { }
public class B : A { }
public class C : B { }
public class Program
{
public static void Main(string[] args)
{
Container<B, A> container = new Container<B, A>(new C());
}
}
728x90
'๐ฅ๏ธ > C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์ด๊ฒ์ด C#์ด๋ค] Chapter11: ์ฐ์ต๋ฌธ์ (0) | 2023.08.22 |
---|---|
[C#] ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ: ์ปฌ๋ ์ (0) | 2023.08.22 |
[์ด๊ฒ์ด C#์ด๋ค] Chapter10: ์ฐ์ต๋ฌธ์ (0) | 2023.08.21 |
[C#] ์ธ๋ฑ์ (0) | 2023.08.21 |
[C#] Collection (0) | 2023.08.21 |