Programming/C#

Programming/C#

Linq Query 실행 타이밍

다음과 같은 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의 모든 값에 대..

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 하도록 설정을 할 수 있다.

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