C#/์ด๊ฒƒ์ด C#์ด๋‹ค

56. ์ผ๋ฐ˜ํ™”ํ•  ๋•Œ, ํ˜•์‹ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ œ์•ฝ์‹œํ‚ค๊ธฐ

Rainbow๐ŸŒˆCoder 2022. 2. 28. 14:52
728x90

์ผ๋ฐ˜ํ™” ํ”„๋กœ๊ทธ๋ž˜๋ฐ(์•„๋ž˜ ์˜์ƒ์„ ์ฐธ๊ณ ํ•˜์—ฌ ํ•™์Šตํ•˜์˜€์Šต๋‹ˆ๋‹ค.)

https://youtu.be/etvC-lj39ws

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[]
728x90