C# 3.0 & LINQ 천호민 Visual C# MVP zmeun.tistory.com
Agenda Part Ⅰ : C# 3.0 New Features Part Ⅲ : LINQ (Language Integrated Query) Part Ⅱ : Functional Programming
C# 3.0 New Features Var Local Variable Anonymous Type Auto-Implemented Properties Object and Collection Initializer Partial Method Extension Method
Demo: C#3.0 New Features
Functional Programming >> Question… : What is the Functional Programming ? Answer is … Paradigm of the Software Programming Avoid State and Mutable Data So, Contrast with the Imperative Programming Style ( 피하다 ) ( 변하기 쉬운 ) ( 대조되다, 대조하다 )( 명령적인 )
Functional Programming >> The Foundation Notions… Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Stric & Lazy Evalution Non-Stric & Lazy Evalution Curry Closure
Functional Programming >> Higher-Order & First-Class Function Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Stric & Lazy Evalution Non-Stric & Lazy Evalution Curry Closure - 함수를 파라미터로 전달 받고 - 결과를 함수로 리턴 - 수학적인 컨셉과 컴퓨터 과학 용어의 차이 -Closure, Curry 와 관련
Functional Programming >> Pure Function Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Stric & Lazy Evaluation Non-Stric & Lazy Evaluation Curry Closure - 다른 프로그램에 영향을 주지 않는 개념 (Have No Side-Effect) - 같은 결과를 리턴하는 함수 - 참조로 전달된 파라미터 값을 수정하지 않고, - 참조로 전달된 객체의 멤버 값을 수정하지 않고, - 외부 객체, 클래스의 static 멤버 들을 수정하지 않는다.
Functional Programming >> Recursion Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Stric & Lazy Evaluation Non-Stric & Lazy Evaluation Curry Closure - 재귀호출이라 부르며 - 자기 자신을 호출한다. - 코드를 단순화 하며 -Higher-Order Function 과 연관
Functional Programming >> Non-Strict & Lazy Evaluation Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Strict & Lazy Evaluation Non-Strict & Lazy Evaluation Curry Closure - 엄격하지 않은 평가 ?, 게으른 평가 ? Ex) f(x)=x^2 + x + 1, g(x,y)=x + y Q) f(g(1,4)) A)1:f(g(1,4)) -> f(1+4) -> f(5) -> 5^ = 31 2:f(g(1,4))->g(1+4)^2+g(1,4)+1 ->(1+4)^2+(1+4)+1->5^2+5+1 = 31 - 함수의 파라미터를 식 (Expression) 으로 표현 - 식의 반복 계산으로 Closure, Lazy Evaluation 필요. -Lazy Evaluation 은 연산결과가 필요 할때 까지 계산을 지연시키는 기술
Functional Programming >> Closure Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Strict & Lazy Evaluation Non-Strict & Lazy Evaluation Curry Closure - 하나의 함수 내 또 다른 함수가 포함되어 있을때 생성 - 외부 함수의 지역 변수를 내부 함수가 참조 가능 - 숨겨진 상태를 처리. OOP 에서도 사용 가능 - 대게 First-Class Function 에서 사용
Functional Programming >> Curry Higher-Order Function Higher-Order Function First-Class Function First-Class Function Pure Function Recursion Non-Strict & Lazy Evaluation Non-Strict & Lazy Evaluation Curry Closure -Curry? 맛있는 인도 음식 ? No! - 다중 파라미터를 갖는 함수에서 실행 - 연결된 함수의 리스트 - 함수가 가지는 N 개의 파라미터에 값을 모두 전달하지 않을 경우 함수가 리턴. 그 외의 경우 함수의 결과 값 리턴. -Closure, Higher-Order Function 과 연관
Demo: Functional Programming’s Foundation Notions
- End of Session 1 -
LINQ (Language-Integrated Query) LINQ Archetecture Lambda Expression Query Expression & Standard Query Operator Expression Tree Deffered Execution
LINQ Archetecture C# 3.0C# 3.0 Visual Basic 9.0Visual Basic 9.0 OthersOthers.NET Language Integrated Query LINQ to Objects LINQ to DataSets LINQ to SQL LINQ to XML Objects XML Relational
LINQ Lambda Expression - 식과 문을 포함하고 대리자 (delegate) 나 식 트리 (Expression Tree) 형식을 만드는데 사용할 수 있는 익명 함수. - C# 프로그래밍 가이드 - public delegate TResult Func (T arg) Syntax : Lambda Operator, “=>” Operator : ex) Func isEven = n => n%2 == 0; Func opSum = (n, m) => n+m;.... max argument to 5
LINQ Query Expression & Standard Query Operator from id in source { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key [ into id query ] from id in source { from id in source | join id in source on expr equals expr [ into id ] | let id = expr | where condition | orderby ordering, ordering, … } select expr | group expr by key [ into id query ] Starts with from Zero or more from, join, let, where, or orderby Ends with select or group by Optional into continuation
LINQ Query Expression & Standard Query Operator from c in customers where c.Region == "WA" select new { c.City, c.Phone }; customers.Where(c => c.Region == "WA").Select(c => new { c.City, c.Phone }); Queries translate to method invocations Where, Join, OrderBy, Select, GroupBy, …
LINQ Expression Tree >> Expression > test = c => c.Region == "WA"; public delegate bool Predicate (T item); ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty(“Region")), Expression.Constant("WA") ); Expression > test = Expression.Lambda >(expr, c);
LINQ Expression Tree >> c => c.Region == "WA"; “c” Expression.Paramete r “c.Region == ‘WA’” Expression.Equal Expression.Property Expression.Constant “c.Region” “’WA’”
Demo: Building LINQ in C#3.0
Simplicity C# 3.0 Feature Map var contacts = from c in customers where c.State == "WA" select new { c.City, c.Phone }; var contacts = from c in customers where c.State == "WA" select new { c.City, c.Phone }; var contacts = customers.Where(c => c.State == "WA").Select(c => new { c.City, c.Phone }); var contacts = customers.Where(c => c.State == "WA").Select(c => new { c.City, c.Phone }); Extension method Anonymous type Lambda expression Query expression Object initializer Local variable type inference Expression tree Automatic properties Partial method
Demo: Building LINQ in C#3.0 Continue…
- End of Session 2 -
Resources MSDN C# 3.0 Overview MSDN.Net Framework MSDN Linq To SQL MSDN Linq Project LINQ in Action Blog WikiPedia
Q&A
Thanks…