Tải bản đầy đủ (.pdf) (77 trang)

Chuyên đề phát triển phần mềm LINQ remoting ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (990.05 KB, 77 trang )

Week 4: LINQ
• Basic concepts
– LINQ requirements
– Concepts
• Types
– LINQ to objects
– LINQ to SQL
– LINQ to Entity
– LINQ to XML
2. New features in langauge
• Generics
• Implicitly Typed Variables
• Object Initializers
• Anonymous Types
• Extension Methods
• Lambda Expressions
2.0 Generics
• the creation of various types of
collection
• type safety
• Binary Code Reuse
– using System.Collections.Generic;
2.1 Implicitly Typed Variables
• Implicitly Typed Variables
– Declare variables without specifying their type
– Strongly type, not variant, not object
– VS will determinr type
• Predict what the compiler will choose
• Intellisense support
– Type inference -> most general
• “3/10/2010” -> string, not date


2.1 Implicitly Typed Variables
• Example
var x = new OledbConnection();
2.1 Implicitly Typed Variables
• Note
– Alwayls declare the type if we know
– Implicitly Typed Variables are suited for LINQ and
anonymous type
2.2 Object Initializers
• Constructor
• Allow to assign values to object properties
(fields) when create object
• We do not have to explicitly invoke a
constructor
• Usefull in any context
– Especially userfull in LINQ expressions
2.2 Object Initializers
• Example
2.3 Anonymous Types
• Implicitly type functionality for objects
– Set property values to object without writing
class definition
– The resulting class has no usable name
– Class name is generated by compiler, inherits
from Object
– The result: an anonymous type that is not
available at source code level
• Also called Projections
2.3 Anonymous Types
• When to user anonymous types

– Need a temporary object to hold related data
– Don’t need method
– If we need a different set of properties for each
declaration
– If we need to change the order of properties for
each declaration
2.3 Anonymous Types
• When not to user anonymous types
– Need to define methods
– Need to define another variable
– Need to shared data across methods
2.4 Extension Methods
• Special kind of Static method
• Allow the addition of methods to an existing
class
– Without creating a new derived type
– Without re-compiling or modifying the original type
• Called The Same way regular methods are called
• Define in static class
2.4 Extension Methods
• Example
2.4 Extension Methods
• Example
2.4.a Delegate
• Delegate
– refers to method.
– When initialize a delegate, we initialize it with
method.
2.4.a delegate: example
2.4.a delegate: why

2.4.a delegate: why
2.4.a delegate: with anonymous
2.4.a delegate: with anonymous:
return to example
2.5 Lambda Expressions
• Lambda Expressions is an Anonymous function
– A funciton without a name
– Perform action , return a single value
– Can be used wherever a delegate type is valid
• Why?
– Allow a function’s caller to define the action
– Not just pass parameters
– Lambda expressions provide a clearer syntax for
anonymous delegates
2.5 Lambda Expressions: delegate
• Syntax
– parameters => expression
– a functional superset of anonymous methods
– Lambda expressions can "infer" parameter types,
even if you omit them
– Lambda expressions can use both statement
blocks and expressions
2.5 Lambda Expressions in C#
• Use lambda operator =>
– Read as “goes to”
– x=>x+3 if x=1 then 4 is returned
– X=> x==5: if x=5, True is returned
– (x,y)=> x+y if x=2, y=4 then 6 is returned
– (x,y)=>x==y if x=2, y=4 then False is returned
2.4.a lambda example

2.4.a delegate: turn back to
example: lambda

×