์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ(์๋ ์์์ ์ฐธ๊ณ ํ์ฌ ํ์ตํ์์ต๋๋ค.)
ch11 ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ (tistory.com) <-๊ฐ์ด ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๊ธฐ ์ข์ ๋ธ๋ก๊ทธ
์ผ๋ฐํ ์ฝ๋์์ ํ์์ ์ ์ฝํ๋ ๋ฌธ๋ฒ์ ๋ค์๊ณผ ๊ฐ๋ค.
ํ์ ๋งค๊ฐ๋ณ์์ ๋ํ ์ผ๋ฐํ ํด๋์ค๋ ์ผ๋ฐํ ๋ฉ์๋์ ๋ชจ๋ ๋์ผํ๊ฒ ์ฌ์ฉ๋๋ค.
where ํ์๋งค๊ฐ๋ณ์ : ์ ์ฝ์กฐ๊ฑด
์ ์ฝ | ์ค๋ช |
where T : struct | T๋ ๊ฐ ํ์ |
where T : class | T๋ ์ฐธ์กฐํ์ |
where T : new() | T๋ ๋ฐ๋์ ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์์ฑ์๊ฐ ์์ด์ผ ํ๋ค. |
where T : ๊ธฐ๋ฐํด๋์ค์ด๋ฆ | T๋ ๋ช ์ํ ๊ธฐ๋ฐ ํด๋์ค์ ํ์ํด๋์ค์ฌ์ผ ํ๋ค. |
where T : ์ธํฐํ์ด์ค์ด๋ฆ | T๋ ๋ช ์ํ ์ธํฐํ์ด์ค๋ฅผ ๋ฐ๋์ ๊ตฌํํด์ผ ํ๋ค. ์ฌ๋ฌ ๊ฐ์ ์ธํฐํ์ด์ค๋ฅผ ๋ช ์ํ ์๋ ์๋ค! |
where T : U | T๋ ๋ ๋ค๋ฅธ ํ์ ๋งค๊ฐ๋ณ์ U๋ก๋ถํฐ ์์๋ฐ์ ํด๋์ค์ฌ์ผ ํ๋ค. |
์ข ์ข ํน์ ์กฐ๊ฑด์ ๊ฐ์ถ ํ์์๋ง ๋์ํ๋ ํ์ ๋งค๊ฐ๋ณ์๊ฐ ํ์ํ ๋๋ ์๋ค.
์ด๋, ํ์ ๋งค๊ฐ๋ณ์์ ์กฐ๊ฑด์ ์ ์ฝ์ ์ค ์ ์๋ค.
MyList<T> ํด๋์ค์ ํ์ ๋งค๊ฐ๋ณ์ T์
Myclass๋ก๋ถํฐ ์์๋ฐ๋ ํ์์ด์ด์ผ ํ ๊ฒ, ์ด๋ผ๋ ์ ์ฝ์ ์ฃผ๋ ค๋ฉด ๋ค์๊ณผ ๊ฐ์ด ํด๋์ค ์ ์ธ๋ฌธ ํค๋์ where์ ์ ์ถ๊ฐํด์ค๋ค.
class MyList<T> where T : MyClass
{
//...
}
์ผ๋ฐํ ๋ฉ์๋
CopyArray<T>()์ ํ์ ๋งค๊ฐ๋ณ์ T์ "๊ฐ ํ์์ด์ด์ผ ํ ๊ฒ"์ด๋ผ๋ ์ ์ฝ์ ๋ค์๊ณผ ๊ฐ์ด ์ค ์ ์๋ค.
void CopyArray<T>(T[] source, T[] target)where T : struct
{
for(int i=0; i<source.Length; i++) target[i]=source[i];
{
์ ์ฝ ์กฐ๊ฑด์ด new()์ ํ์ ๋งค๊ฐ๋ณ์ U์ผ ๊ฒฝ์ฐ์๋ ๋ค์๊ณผ ๊ฐ๋ค.
๋ค์ ์ฝ๋์ CreateInstance<T>() ๋ฉ์๋๋ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ๊ฐ์ง ์ด๋ค ํด๋์ค์ ๊ฐ์ฒด๋ผ๋ ์์ฑํด์ค๋ค.
์ด ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๋ ํด๋์ค๋ฅผ ํ์ ๋งค๊ฐ๋ณ์์ ๋๊ธฐ๋ฉด ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฌ๋ค.
public static T CreateInstance<T>() where T : new()
{
return new T();
}
๋ค์์ ์์ ์ฝ๋์์ ์ฌ์ฉ๋๋ ํ์ ๋งค๊ฐ๋ณ์ U๋ก๋ถํฐ ์์๋ฐ๋ ํ์์ผ๋ก ์ ์ฝ์กฐ๊ฑด์ ์ฃผ๋ ์์ด๋ค.
๋ค์ ์ฝ๋์ CopyArray<T>()๋ ์์ ํด๋์ค์ธ BaseArray<U>์ ํ์ ๋งค๊ฐ๋ณ์ U๋ก๋ถํฐ T๊ฐ ์์๋ฐ์์ผ ํ ๊ฒ์ ๊ฐ์ ํ๊ณ ์๋ค.
class BaseArray<U> where U : Base
{
public U[] Array{get; set;}
public BaseArray(int size)
{
Array = new U[size];
}
public void CopyArray<T>(T[] Source) where T : U
{
Source.CopyTo(Array,0);
}
}
<์์ >
using System;
using System.Collections;
namespace ConstraintsOnTypeParameters
{
class StructArray<T> where T : struct
{
public T[] Array { get; set; }
public StructArray(int size)
{
Array = new T[size];
}
}
class Refarray<T> where T : class
{
public T[] Array { get; set; }
public Refarray(int size)
{
Array = new T[size];
}
}
class Base { }
class Derived : Base { }
class BaseArray<U> where U : Base
{
public U[] Array { get; set; }
public BaseArray(int size)
{
Array = new U[size];
}
public void CopyArray<T>(T[] Source) where T : U
{
Source.CopyTo(Array, 0);
}
}
interface IDictionary1<T> { }
interface IDictionary2<T> { }
class SampleClass1<T> where T : IDictionary1<T> { }
class SampleClass2<T> where T : IDictionary2<T> { }
class MainApp
{
public static T CreateInstance<T>() where T : new()
{
return new T();
}
static void Main(string[] args)
{
// ๊ฐ ํ์ ์ผ๋ฐํ ํด๋์ค
// - struct๋ ๊ฐ ํ์์ผ๋ก ์ ์ํ๋ค.
// - StructArray<int>()์ ์ธ์คํด์ค๋ ๋ฐฐ์ด์ ํฌ๊ธฐ๋ค.
StructArray<int> a = new StructArray<int>(3);
a.Array[0] = 0;
a.Array[1] = 1;
a.Array[2] = 2;
//a.Array[3] = 3; ํฌ๊ธฐ๊ฐ 3์ธ ๋ฐฐ์ด์ด๋ฏ๋ก ๋ฒ์๊ฐ ๋์ด๊ฐ์ ์๋ฌ
//a.Array[2] = "์๋ฌ"; intํ์์ผ๋ก ์ ์ธํ์ผ๋ฏ๋ก stringํ์ ์๋ฌ
Console.WriteLine("--------------------------------");
Console.WriteLine($"a.Array[0] : {a.Array[0]}");
Console.WriteLine($"a.Array[1] : {a.Array[1]}");
Console.WriteLine($"a.Array[2] : {a.Array[2]}");
Console.WriteLine("--------------------------------");
Console.WriteLine();
Refarray<StructArray<double>> b = new Refarray<StructArray<double>>(3);
b.Array[0] = new StructArray<double>(5);
b.Array[1] = new StructArray<double>(10);
b.Array[2] = new StructArray<double>(1005);
b.Array[0].Array[4] = 3.14d;
b.Array[1].Array[9] = 2.86d;
b.Array[2].Array[1004] = 101.1004d;
Console.WriteLine("--------------------------------");
Console.WriteLine($"b.Array[0].Array[4] : {b.Array[0].Array[4]}");
Console.WriteLine($"b.Array[1].Array[9] : {b.Array[1].Array[9]}");
Console.WriteLine($"b.Array[2].Array[1004] : {b.Array[2].Array[1004]}");
Console.WriteLine("--------------------------------");
Console.WriteLine();
BaseArray<Base> c = new BaseArray<Base>(3);
c.Array[0] = new Base();
c.Array[1] = new Derived();
c.Array[2] = CreateInstance<Base>();
Console.WriteLine("--------------------------------");
Console.WriteLine($"c.Array[0] : {c.Array[0]}");
Console.WriteLine($"c.Array[1] : {c.Array[1]}");
Console.WriteLine($"c.Array[2] : {c.Array[2]}");
Console.WriteLine("--------------------------------");
Console.WriteLine();
BaseArray<Derived> d = new BaseArray<Derived>(3);
d.Array[0] = new Derived(); //Base ํ์์ ์ฌ๊ธฐ์ ํ ๋นํ ์ ์๋ค.
d.Array[1] = CreateInstance<Derived>();
d.Array[2] = CreateInstance<Derived>();
Console.WriteLine("--------------------------------");
Console.WriteLine($" d.Array[0] : {d.Array[0]}");
Console.WriteLine($" d.Array[1] : {d.Array[1]}");
Console.WriteLine($" d.Array[2] : {d.Array[2]}");
Console.WriteLine("--------------------------------");
Console.WriteLine();
BaseArray<Derived> e = new BaseArray<Derived>(3);
e.CopyArray<Derived>(d.Array); //e์ ๋ค๊ฐ d๋ฅผ ๋ฎ์ด์์ด๋ค.
Console.WriteLine($" d.Array : {d.Array}");
}
}
}
<์ถ๋ ฅ ๊ฒฐ๊ณผ>
--------------------------------
a.Array[0] : 0
a.Array[1] : 1
a.Array[2] : 2
--------------------------------
--------------------------------
b.Array[0].Array[4] : 3.14
b.Array[1].Array[9] : 2.86
b.Array[2].Array[1004] : 101.1004
--------------------------------
--------------------------------
c.Array[0] : ConstraintsOnTypeParameters.Base
c.Array[1] : ConstraintsOnTypeParameters.Derived
c.Array[2] : ConstraintsOnTypeParameters.Base
--------------------------------
--------------------------------
d.Array[0] : ConstraintsOnTypeParameters.Derived
d.Array[1] : ConstraintsOnTypeParameters.Derived
d.Array[2] : ConstraintsOnTypeParameters.Derived
--------------------------------
d.Array : ConstraintsOnTypeParameters.Derived[]
'C# > ์ด๊ฒ์ด C#์ด๋ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
58. ์ผ๋ฐํ ์ปฌ๋ ์ (0) | 2022.02.28 |
---|---|
57. ์ผ๋ฐํ ํ๋ก๊ทธ๋๋ฐ ์์ ๋ชจ์ (0) | 2022.02.28 |
55. foreach๊ฐ ๊ฐ๋ฅํ ๊ฐ์ฒด ๋ง๋ค๊ธฐ (0) | 2022.02.27 |
54. ์ธ๋ฑ์ (0) | 2022.02.25 |
53. ์ปฌ๋ ์ (ArrayList, Queue, Stack, Hashtable) (0) | 2022.02.25 |