280 likes | 571 Views
C# 3.0 & LINQ. 천호민 Visual C# MVP zmeun.tistory.com. Agenda. Part Ⅰ: C# 3.0 New Features. Part Ⅱ : Functional Programming. Part Ⅲ : LINQ (Language Integrated Query). C# 3.0 New Features. Var Local Variable. Anonymous Type. Auto-Implemented Properties.
E N D
C# 3.0 & LINQ 천호민 Visual C# MVP zmeun.tistory.com
Agenda Part Ⅰ: C# 3.0 New Features Part Ⅱ : Functional Programming Part Ⅲ : LINQ (Language Integrated Query)
C# 3.0 New Features Var Local Variable • Anonymous Type • Auto-Implemented Properties • Object and Collection Initializer • Partial Method • Extension Method
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… First-Class Function Higher-Order Function Recursion Pure Function Curry Non-Stric & Lazy Evalution Closure
Functional Programming >> Higher-Order & First-Class Function First-Class Function Higher-Order Function Recursion Pure Function -함수를 파라미터로 전달 받고 -결과를 함수로 리턴 -수학적인 컨셉과 컴퓨터 과학 용어의 차이 -Closure, Curry와 관련 Curry Non-Stric & Lazy Evalution Closure
Functional Programming >> Pure Function First-Class Function -다른 프로그램에 영향을 주지 않는 개념 (Have No Side-Effect) -같은 결과를 리턴하는 함수 -참조로 전달된 파라미터 값을 수정하지 않고, -참조로 전달된 객체의 멤버 값을 수정하지 않고, -외부 객체, 클래스의 static 멤버 들을 수정하지 않는다. Higher-Order Function Recursion Pure Function Curry Non-Stric & Lazy Evaluation Closure
Functional Programming >> Recursion First-Class Function -재귀호출이라 부르며 -자기 자신을 호출한다. -코드를 단순화 하며 -Higher-Order Function과 연관 Higher-Order Function Recursion Pure Function Curry Non-Stric & Lazy Evaluation Closure
Functional Programming >> Non-Strict & Lazy Evaluation -엄격하지 않은 평가?, 게으른 평가? Ex) f(x)=x^2 + x + 1, g(x,y)=x + y Q) f(g(1,4)) 1:f(g(1,4)) -> f(1+4) -> f(5) -> 5^2 + 5 + 1 = 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은 연산결과가 필요 할때 까지 계산을 지연시키는 기술 First-Class Function Higher-Order Function Recursion Pure Function Curry Non-Strict & Lazy Evaluation Closure
Functional Programming >> Closure First-Class Function Higher-Order Function -하나의 함수내 또 다른 함수가 포함되어 있을때 생성 -외부 함수의 지역 변수를 내부 함수가 참조 가능 -숨겨진 상태를 처리.OOP에서도 사용 가능 -대게 First-Class Function에서 사용 Recursion Pure Function Curry Non-Strict & Lazy Evaluation Closure
Functional Programming >> Curry First-Class Function Higher-Order Function -Curry? 맛있는 인도 음식? No! -다중 파라미터를 갖는 함수에서 실행 -연결된 함수의 리스트 -함수가 가지는 N개의 파라미터에 값을 모두 전달하지 않을 경우 함수가 리턴. 그 외의 경우 함수의 결과 값 리턴. -Closure, Higher-Order Function과 연관 Recursion Pure Function Curry Non-Strict & Lazy Evaluation Closure
LINQ (Language-Integrated Query) LINQ Archetecture • Lambda Expression • Query Expression & Standard Query Operator • Expression Tree • Deffered Execution
<book> <title/> <author/> <year/> <price/> </book> Relational Objects XML LINQ Archetecture .NET Language Integrated Query C# 3.0 Visual Basic 9.0 Others LINQ toObjects LINQ toDataSets LINQ toSQL LINQ toXML
LINQ Lambda Expression • 식과 문을 포함하고 대리자(delegate)나 • 식 트리(Expression Tree) 형식을 • 만드는데 사용할 수 있는 익명 함수. • - C# 프로그래밍 가이드 - Syntax : public delegate TResult Func<T, TResult>(T arg) Lambda Operator , “=>” Operator : ex) Func<int, bool> isEven = n => n%2 == 0; Func<int, int, int> opSum = (n, m) => n+m; . . . . max argument to 5
LINQ Query Expression & Standard Query Operator Starts with from Zero or more from, join, let, where, or orderby fromidinsource { fromidinsource | joinidinsourceonexprequalsexpr [ intoid ] | letid = expr | wherecondition | orderbyordering, ordering, … } selectexpr | groupexprbykey [ intoidquery ] Ends with select or groupby Optional into continuation
LINQ Query Expression & Standard Query Operator • Queries translate to method invocations • Where, Join, OrderBy, Select, GroupBy, … 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 });
LINQ Expression Tree >> public delegate bool Predicate<T>(T item); Expression<Predicate<Customer>> test = c => c.Region == "WA"; ParameterExpression c = Expression.Parameter(typeof(Customer), "c"); Expression expr = Expression.Equal( Expression.Property(c, typeof(Customer).GetProperty(“Region")), Expression.Constant("WA") ); Expression<Predicate<Customer>> test = Expression.Lambda<Predicate<Customer>>(expr, c);
LINQ Expression Tree >> c => c.Region == "WA"; “c” Expression.Parameter “c.Region == ‘WA’” Expression.Equal “c.Region” Expression.Property “’WA’” Expression.Constant
Simplicity C# 3.0 Feature Map Query expression var contacts = from c in customers where c.State == "WA" select new { c.City, c.Phone }; Expression tree Local variable type inference Automatic properties Lambda expression var contacts = customers .Where(c => c.State == "WA") .Select(c => new { c.City, c.Phone }); Partial method Extension method Anonymous type Object initializer
Resources • MSDN C# 3.0 Overviewhttp://msdn.microsoft.com/ko-kr/library/bb308966(en-us).aspx • MSDN .Net Frameworkhttp://msdn.microsoft.com/ko-kr/library/w0x726c2.aspx • MSDN Linq To SQLhttp://msdn.microsoft.com/ko-kr/library/bb386976.aspx • MSDN Linq Projecthttp://msdn.microsoft.com/ko-kr/netframework/aa904594(en-us).aspx • LINQ in Action Bloghttp://linqinaction.net/blogs/default.aspx • WikiPediahttp://en.wikipedia.org/wiki/Functional_programming