Programming/C#

Programming/C#

Event & Delegate 기초

Delegate : 대리자, 특정 지정된 함수를 대신하여 수행해주는 역할, c++ 함수 포인터와 같은 역할. Delegate는 내부적으로 MulticastDelegate를 상속하므로, 여러 Listener (EventHandler)를 포함 할 수 있다. Event : Delegate 를 event 한정자로 수식하여 만드는 것. Delegate 와 크게 다른 점은 이벤트 외부에서 직접 사용할 수 없다는 것이다. event는 public 한정자로 선언되어 있어도 자신이 선언되어 있는 클래스 외부에서는 호출이 불가능하다. Delegate 와 EventHandler의 형식은 동일해야 한다. Delegate 만을 이용하여 Eventhandler 수행 del1에 del2 를 추가하..

Programming/C#

Delegate Simple Example Code

MSDN 참조 코드 : https://msdn.microsoft.com/ko-kr/library/900fyy8e.aspx// Declare delegate -- defines required signature: delegate double MathAction(double num); class DelegateTest { // Regular method that matches signature: static double Double(double input) { return input * 2; } static void Main() { // Instantiate delegate with named method: MathAction ma = Double; // Invoke delegate ma: double mu..

Programming/C#

Generic Reflection

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..

Programming/C#

Generic Constraint Covariace and Contravariance

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() 를 사용하지 못한다. 이를 가능하기 위해 제..

Programming/C#

Generic Delegate

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..

Programming/C#

Generic Method - Extension Method

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..

Programming/C#

Generics

Generics Study 참조 : Pluralsight

Programming/C#

Interface class 에 대한 생각들..

Maintainable : - base class로써 상속 클래스에서 꼭 구현이 필요한 내용들을 알려준다.- compile 단계에서 상속 클래스의 오류 발생 가능성을 줄여준다. Extensible : - 공통된 기능을 사용하는 상속 클래스들을 Factory pattern을 이용하여 쉽게 접근 가능하도록 한다.- IBaseClass 를 만들고 이를 상속하는 여러 클래슬들을 ChildClass1, ChildClass2 --- 있다고 가정할 때에.. 모든 클래스의 접근을 IBaseClass 객체를 이용하여 참조가 가능하므로 코드의 반복성을 줄일 수 있다.

Programming/C#

App.Config 사용하기..

.Net Framework 의 프로젝트에는 App.config 파일을 추가할 수 있다.app.config 파일을 작성하고 build를 수행하면 실행 파일의 경로에 app.config 파일이 그대로 복사가 된다. 프로그램이 실행이 될때에 app.config파일의 내용을 참조하여 runtime 시에 사용할 수 있다. 1. key 값을 설정할 수 있다. 아래와 같이 사용할 있다. 2. Library Probing 참조하는 dll 라이브러리의 경로를 임의의 지정된 폴더에서 probing 하도록 설정을 할 수 있다.

Programming/C#

Abstraction vs Interface Class??

클래스 상속을 위해 부모 클래스를 생성할 때 고려할 수 있는 대상은 3가지가 될 수 있다. 1. standard class 2. abstract class 3. interface class 1번의 경우는 일반 클래스로 자식 클래스에서 override를 수행할 때에 부모 클래스의 함수가 수행 되므로, 특정 자식 클래스에 대한 함수를 따로 구현 할 떄에 이를 compile 단계에서 알수가 없다. 그와 반대로 2,3번의 경우는 위의 상황에 대해 compile 단계에서 에러를 발생한다. 그럼, 어떤 상황을 생각 했을 때에 abstract 와 interface 를 선택할 것인가?? 차이점을 살펴 보자. - abstract 클래스는 구현이 가능한 클래스이나, interface의 경우는 정의만 되어진 클래스이다. -..

RichardBang
'Programming/C#' 카테고리의 글 목록 (4 Page)