๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ–ฅ๏ธ/C#

[C#] ์ผ๋ฐ˜ํ™” ํ”„๋กœ๊ทธ๋ž˜๋ฐ: ๋ฉ”์„œ๋“œ, ํด๋ž˜์Šค, ์ œ์•ฝ์กฐ๊ฑด

by HanaV 2023. 8. 22.
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

"); wcs_do();