C# Generic
Unity3D/C# 2019. 1. 23. 00:52반응형
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 29 30 31 32 33 34 | using System; namespace ConsoleApp4 { class Program { static void Main(string[] args) { var book = new Book() {Isbn = "1111", Title = "C# Advanced"}; //var numbers = new List(); //numbers.Add(10); //var books = new BookList(); //books.Add(book); //var number = new GenericList<int>(); //number.Add(10); //var books = new GenericList<Book>(); //books.Add(new Book()); //var dictionary = new GenericDictionary<string, Book>(); //dictionary.Add("1234", new Book()); var number = new ConsoleApp4.Nullable<int>(); Console.WriteLine("HasValue: " + number.HasValue); Console.WriteLine("Value: " + number.GetValueOrDefault()); Console.ReadKey(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp4 { public class List { public void Add(int number) { throw new NotImplementedException(); } public int this[int index] { get { throw new NotImplementedException(); } } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System; namespace ConsoleApp4 { public class BookList { public void Add(Book book) { throw new NotImplementedException(); } public Book this[int index] { get { throw new NotImplementedException(); } } } } | cs |
1 2 3 4 5 6 7 8 | namespace ConsoleApp4 { public class Book { public string Isbn { get; set; } public string Title { get; set; } } } | cs |
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 | using System; namespace ConsoleApp4 { public class GenericList<T> { public void Add(T value) { } public T this[int index] { get { throw new NotImplementedException(); } } } public class GenericDictionary<TKey, TValue> { public void Add(TKey key, TValue value) { } } } | cs |
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 29 30 31 32 | namespace ConsoleApp4 { public class Nullable<T> where T : struct { private object _value; public Nullable() { } public Nullable(T value) { _value = value; } public bool HasValue { get { return _value != null; } } public T GetValueOrDefault() { if (HasValue) { return (T)_value; //unboxing } return default(T); } } } | cs |
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 29 30 31 32 | using System; namespace ConsoleApp4 { // where T : IComparable interface // where T : Product sub classes // where T : struct value type // where T : class reference type // where T : new() default constructor public class Utilities<T> where T : IComparable, new() { public int Max(int a, int b) { return a > b ? a : b; } //public T Max<T>(T a, T b) where T : IComparable //{ // return a.CompareTo(b) > 0 ? a : b; //} public T Max(T a, T b) { return a.CompareTo(b) > 0 ? a : b; } public void DoSomething(T value) { var obj = new T(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 | namespace ConsoleApp4 { public class DiscountCalculator<TProduct> where TProduct : Product { public float CalculateDiscount(TProduct product) { return product.Price; } } } | cs |
1 2 3 4 5 6 7 8 | namespace ConsoleApp4 { public class Product { public string Title { get; set; } public float Price { get; set; } } } | cs |
반응형
'Unity3D > C#' 카테고리의 다른 글
C# Array Value Type or Reference (0) | 2019.03.12 |
---|---|
CLR(Common Language Runtime) (0) | 2019.03.12 |
C# Event & delegate (0) | 2019.01.23 |
ReSharper (0) | 2019.01.23 |
C# Split List (0) | 2019.01.22 |