●/섹션 7. 유용한 문법

01.Generic 일반화

한국 /프랑스 웹툰 리뷰 2021. 7. 13. 23:32
728x90
using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //나만의 자료구조 - 특별한 List를 만들어보자


        class MyIntList     //비효율
        {
            int[] arr = new int[10];
        }

        class MyShortList   //비효율
        {
            short[] arr = new short[10];
        }

        class MyFloatList   //비효율
        {
            float[] arr = new float[10];
        }

        class MyMonsterList //비효율
        {
            Monster[] arr = new Monster[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //object : 어떤 자료형 타입이건 다 소화가 가능하다 
            //(int나 string등 자료형들은 object에게 상속을 받아 쓰는 경우니, object가 조상이다)
            object obj = 3;
            object obj2 = "hello world";

            //var : 어떤 자료형 타입이건 다 소화가 가능하다
            var obj3 = 3;
            var obj4 = "hello world";

            //object와 var의 차이점 : objct위에 마우스를 올려보면 Object 형이라고 나온다.
            //                        하지만 var의 경우, 뒤에 뭐가 오냐에 따라 int가 오기도, string이 오기도 한다.
            int num = (int)obj;
            string str = (string)obj2;

            //object로 모든 자료형을 나타낼 수 있으면, 그냥 이거 쓰면 되지 않을까?
            //아니다. 속도가 느리다.

            //이 경우, 힙이 아니라 스택이다. 굉장히 간편하게 쓸 수 있다.
            int number = 3;

            //하지만 object의 경우, 참조 타입으로만 동작을 한다. 즉, 힙 메모리에 할당 한다.
            object obj5 = 1;

            //object 작업 (= 무거운 작업)
            
            //넣고 (= 박싱)
            object obj6 = 1;
            //꺼내기 (= 언박싱)
            int aaa = (int)obj6;
        }
    }
}

 

 

Generic - 일반화(클래스 편)

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T>
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();
            MyList<short> mySShortList = new MyList<short>();
            MyList<Monster> myMonsterList = new MyList<Monster>();
        }
    }
}

 

Generic - 일반화(함수 편)

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(함수 편)
        static void Test<T>(T input)
        {

        }

        static void Main(string[] args)
        {
            Test<int>(3);
            Test<float>(3.0f);
        }
    }
}

 

 

Generic - 일반화(클래스 편) - 아이템

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T>
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];

            public T GetItem(int i)
            {
                return arr[i];
            }
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();
            int item = myIntList.GetItem(0);

            MyList<short> mySShortList = new MyList<short>();
            MyList<Monster> myMonsterList = new MyList<Monster>();
        }
    }
}

 

 

Generic - 일반화(클래스 편) - 조건 추가

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T> where T : struct    //반드시 값 형식이어야 한다.
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();
            MyList<short> mySShortList = new MyList<short>();
            MyList<Monster> myMonsterList = new MyList<Monster>();      //빨간줄
        }
    }
}

where T : struct 를 추가하면, 반드시 값 형식이어야 한다.

 

 

 

Generic - 일반화(클래스 편) - 조건추가2

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T> where T : class    //반드시 참조 형식이어야 한다.
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();              //빨간줄
            MyList<short> mySShortList = new MyList<short>();       //빨간줄
            MyList<Monster> myMonsterList = new MyList<Monster>();
        }
    }
}

where T : struct 를 추가하면, 반드시 참조 형식이어야 한다.

 

 

Generic - 일반화(클래스 편) - new()

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T> where T : new()    //반드시 어떠한 인자도 받지않는 기본 생성자가 있어야 한다. 
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();
            MyList<short> mySShortList = new MyList<short>();
            MyList<Monster> myMonsterList = new MyList<Monster>();
        }
    }
}

 

Generic - 일반화(클래스 편) - class이름

 

using System;
using System.Collections.Generic;

namespace Csharp
{
    class Program
    {
        //Generic - 일반화(클래스 편)

        // T = 템플릿, 타입의 약자. 조커 같은 존재. var나 object 같은 존재.
        class MyList<T> where T : Monster    //T에 어떤것이든 넣을 수 있지만, T는 반드시 Monster 혹은 Monster를 상속받은 클래스여야 한다. 
        {
            //object[] arr = new object[10];
            T[] arr = new T[10];
        }

        class Monster
        {

        }

        static void Main(string[] args)
        {
            //MyList<T> : 원래 <꺽쇠> 자리에는 자료형 int, string 등이 들어가는 자리
            MyList<int> myIntList = new MyList<int>();              //빨간줄
            MyList<short> mySShortList = new MyList<short>();       //빨간줄
            MyList<Monster> myMonsterList = new MyList<Monster>();
        }
    }
}
728x90