C#/C# Concept
[C#] 제네릭
군우
2018. 3. 19. 12:39
제네릭
리스트 클래스
List<int> list1 = new List<int>();
이 <> 이거를 제네릭이라고함 .
<> 안에는 내부 식별자를 지정하게함.
java랑 비슷함
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CSStudy_generic1 { class Program { static void Main(string[] args) { Person<int> intp = new Person<int>(4); Person<string> strp = new Person<string>("3"); Person<long> longp = new Person<long>(333); Person<double> doublep = new Person<double>(3.33); // 이거 float은 왜안되지? } class Person<T> { public Person(T num) { Console.WriteLine(num); } } } } | cs |
제네릭형 매개변수의 제한조건
where T : struct // T가 값형이여야 함.
where T : class // T가 참조형이여야 함.
where T : new() // T가 매개변수가 없는 생성자를 가지고 있어야함
where T : MyClass // T가 MyClass의 파생클래스여야함.