본문 바로가기
C#

Generic

by Mostlove 2023. 7. 10.
728x90
반응형

namespace CSharp1
{
    internal class Program
    {
        class MyList<T> where T : new()
        {
            T[] arr = new T[10];
            public T GetItem(int i)
            {
                return arr[i];
            }
        }
        class Monster
        {

        }
        static void Test<T>(T input)
        {

        }

        static void Main(string[] args)
        {
            /*var objV = 3;
            var objV2 = "Hello World";
            object obj = 3;//참조 타입
            object objs = "Hello World";
            int num = (int)obj;//박싱 언박싱
            string str = (string)objs;*/
            MyList<int> list = new MyList<int>();
            int item = list.GetItem(0);

            MyList<short> list1 = new MyList<short>();
            MyList<Monster> list2 = new MyList<Monster>();

            Test<int>(3);
            Test<float>(3f);

        }
    }
}

반응형

'C#' 카테고리의 다른 글

Property(프로퍼티)  (0) 2023.07.10
Interface  (0) 2023.07.10
Dictionary  (0) 2023.07.09
List  (0) 2023.07.09
다차원 배열  (0) 2023.07.09