1. DebuggerDisplay Attribute : Debugging 시에 데이터의 속성을 사용자가 원하는 형태로 출력할 수 있는 속성을 지정한다. 예) [DebuggerDisplay("This person is called {Name} and is {AgeInYears} years old")] class PersonWithDebuggerDisplay { [DebuggerDisplay("{AgeInYears} years old")] public int AgeInYears { get; set; } public string Name { get; set; } } 사용 범위 : Classes , Structs, Delegates, Enums, Fields, Properties, Assemblies 2. De..
Value Type 을 사용함에 있어서 .Net Framework를 빠르게 만드는 Tip을 설명한다. 우선 Value Type 과 Reference Type은 메모리 구조적으로 차이가 있다. 아래에서 보면 Reference Type은 두개의 헤더 필드를 갖는다. Object header Word는 32bit에서는 4byte, 64bit에서는 8byte의 길이를 갖는다. 이러한 차이로 객체를 수없이 많이 사용할 때에는 메모리의 차이가 극심해 진다. struct/class Point2 { int X, int Y } 만약 위의 Point2와 같은 객체를 1만개 생성할 때에는 Reference type의 경우는 32만 byte를 갖게 되며 Value Type의 경우에는 8만 byte를 갖는다. 두 type의 메..
다음과 같은 foreach 구문이 있다. foreach (var c in customerList) { if (c.CustomerId == customerId) { foundCustomer = c; break; } } 위의 foreach 구문과 같은 내용을 Linq를 이용하여 표현하면 아래와 같다. var query = from c in customerList where c.CustomerId == customerId select c; foundCustomer = query.First(); 두 구문을 비교해 보면, foreach의 경우는 loop를 돌면서 customerId와 같으면 loop를 빠져 나올수 있다. 하지만, 직관적인 관점에서 봤을 때 linq 구문에서는 customerList의 모든 값에 대..
Delegate : 대리자, 특정 지정된 함수를 대신하여 수행해주는 역할, c++ 함수 포인터와 같은 역할. Delegate는 내부적으로 MulticastDelegate를 상속하므로, 여러 Listener (EventHandler)를 포함 할 수 있다. Event : Delegate 를 event 한정자로 수식하여 만드는 것. Delegate 와 크게 다른 점은 이벤트 외부에서 직접 사용할 수 없다는 것이다. event는 public 한정자로 선언되어 있어도 자신이 선언되어 있는 클래스 외부에서는 호출이 불가능하다. Delegate 와 EventHandler의 형식은 동일해야 한다. Delegate 만을 이용하여 Eventhandler 수행 del1에 del2 를 추가하..
Reflection 은 모든 클래스가 상속받는 object의 속성들을 이용하여 동적으로 객체를 생성할 때 사용할 수 있다. 간단하게 Generic 클래스의 객체와 generic 함수를 reflection을 이용하여 생성하는 방법이다. namespace ReflectIt { class Program { static void Main(string[] args) { var employeeList = CreateCollection(typeof(List), typeof(Employee)); Console.Write(employeeList.GetType().Name); var genericArgs = employeeList.GetType().GetGenericArguments(); foreach (var arg i..
Generic Constraints Generic parameter에 대한 제한자를 설정한다. Generic class 선언할 때에 꼭 reference type의 class만 사용이 필요한 경우가 있다. 이를 위해서 Generic Parameter 를 로 가정할 때에, T에 대한 제한자를 generic constraint라고 한다. class MyGenericClass where T : class { } 위의 where T : class 선언으로 인해 T는 reference type의 클래스만 입력받을 수 있다. class 뿐만 아니라 제한자는 class, struct , IEntity 와 같이 될 수 있다. 추가 적으로 generic class는 new() 를 사용하지 못한다. 이를 가능하기 위해 제..
public delegate void Printer(T data); public static class BufferExtension { public static void Dump(this IBuffer buffer, Printer print) { foreach (var item in buffer) { print(item); } } } Action , Func , Predicate Action : 적어도 한개 이상의 generic parameter를 갖고 void return Fcun : 적어도 한개 이상의 generic parameter를 갖고 마지막 generic parameter의 형태로 return 한다. Predicate : 한개의 generic parameter를 갖고 bool 타입의 retur..
Generic Method 를 이용한 방법 public IEnumerable AsEnumberatorOf() { var converter = TypeDescriptor.GetConverter(typeof(T)); foreach(var item in _queue) { var result = converter.ConvertTo(item, typeof(TOutput)); yield return (TOutput)result; } } 위의 방법을 Extension Method로 변형하는 방식 참고 ) Extension Method 작성하는 방법 method를 선언하되, static 한정자로 수식해야 하며, 첫번째 매개변수는 반드시 this 키워드와 함께 확장하고자 하는 클래스(형식)의 인스턴스여야 한다. name..