100% found this document useful (1 vote)
327 views96 pages

C Sharp 3

C# 3.0 introduced many new language features to support LINQ, including implicit typing, anonymous types, extension methods, lambda expressions, query expressions, and expression trees. These features allow for more concise coding of objects, initialization of collections and objects, extension of existing types, and representation of code as data through expression trees to build queries against different data sources.

Uploaded by

api-3845693
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
100% found this document useful (1 vote)
327 views96 pages

C Sharp 3

C# 3.0 introduced many new language features to support LINQ, including implicit typing, anonymous types, extension methods, lambda expressions, query expressions, and expression trees. These features allow for more concise coding of objects, initialization of collections and objects, extension of existing types, and representation of code as data through expression trees to build queries against different data sources.

Uploaded by

api-3845693
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1/ 96

C sharp 3.

0
• Implicit variable declaration
• Anonymous Types
• Extension Methods
• Automatic Properties
• Object Initializers
• Collection Initializers
• Lambda Expressions
• Query Expressions
• Expression Trees
• LINQ to Collections
• LINQ to DataSet
• LINQ to SQL
• Partial Methods
• LINQ to XML
Implicit variable declaration
Implicit declaration
Only within method can declare this
Not even with interfaces also
Not even as parameter also
Take home of Implicit Variable

• For local stack based variables

• Its nothing like late binding or anything of that kind.

• Compiler is infers the type.

• Strongly associate, linked or used with Anonymous type


Anonymous Types
Anonymous Types

• Anonymous Types allow you to create a class structure


on the fly.

• It uses Implicit Variables


Creating arrays of Anonymous Types
Anonymous type gels and works well
with the Lambda Expressions
Extension Methods
Extension Methods
Extension Methods

• Extension methods allow developers to add new methods to the


public contract of an existing CLR type, without having to sub-class
it or recompile the original type.

• Extension methods are a new feature that allows you to enhance an


existing class by adding a new method to it without modifying the
actual code for the class. This is especially useful when using LINQ
because several extension methods are available in writing LINQ
query expressions.
Only public ones gets extended

Note how the static method above has a "this" keyword before the first parameter
argument of type string. This tells the compiler that this particular Extension Method
should be added to objects of type “int".
Automatic Property
Note about that we aren't actually adding any logic in the getters/setters of
our properties – instead we just get/set the value directly to a field.
Automatic Properties
• When the C# "Orcas" compiler encounters an empty get/set
property implementation like above, it will now automatically
generate a private field for you within your class, and
implement a public getter and setter property implementation
to it.

• With the C# and VB "Orcas" compilers you can now take


advantage of a great "syntactic sugar" language feature called
"object Initializers"
object Initializers
Object Initialization with Complex
Type
Collection Initializers
Collection Initializer

• Initial the collection in a single line of code.

• Collection need to implement Icollection<T>


As good as doing obj.add() in the background
Query Expression
LINQ
Language-Integrated Query
(LINQ)
LINQ Language
• Language-Integrated Query (LINQ) is a groundbreaking
innovation in Visual Studio 2008 and the .NET
Framework version 3.5 that bridges the gap between
the world of objects and the world of data.

• Traditionally, queries against data are expressed as


simple strings without type checking at compile time or
IntelliSense support.

• LINQ is the project name for a set of extensions to the


.NET Framework that provide a generic approach to
querying data from different data sources.
Traditionally
• Traditionally, queries against data are expressed as
simple strings without type checking at compile time or
IntelliSense support.

• Furthermore, you have to learn a different query


language for each type of data source: SQL databases,
XML documents, various Web services, and so on.

• LINQ makes a query a first-class language construct in


C# and Visual Basic. You write queries against strongly
typed collections of objects by using language keywords
and familiar operators.
The following illustration shows a
partially-completed LINQ query
against a SQL Server database in C#
with full type checking and
IntelliSense support.
LINQ
• In Visual Studio you can write LINQ queries in Visual
Basic or C# with SQL Server databases, XML
documents, ADO.NET Datasets, and any collection of
objects that supports IEnumerable or the generic
IEnumerable<(Of <(T>)>) interface.

• LINQ support for the ADO.NET Entity Framework is also


planned.

• LINQ providers are being written by third parties for


many Web services and other database
implementations.
Introduction to LINQ Queries
• A query is an expression that retrieves data from a data
source. Queries are usually expressed in a specialized
query language. (example SQL for relational databases
and XQuery for XML. )

• Therefore, developers have had to learn a new query


language for each type of data source or data format that
they must support.

• LINQ simplifies this situation by offering a


consistent model for working with data across
various kinds of data sources and formats.
Take Home
• In a LINQ query, you are always working with objects.

• You use the same basic coding patterns to query and


transform data in XML documents, SQL databases,
ADO.NET Datasets, .NET collections, and any other
format for which a LINQ provider is available.
Three Parts of a Query Operation
All LINQ query operations consist of three distinct actions:

• Obtain the data source.


• Create the query.
• Execute the query.
Take home 2

• The from clause specifies the data source, the where


clause applies the filter, and the select clause specifies
the type of the returned elements.

• The query variable itself only stores the query


commands. The actual execution of the query is deferred
until you iterate over the query variable in a foreach
statement. This concept is referred to as deferred
execution.
Forcing Immediate Execution

• Queries that perform aggregation functions over a range


of source elements must first iterate over those
elements. Examples of such queries are Count, Max,
Average, and First. These execute without an explicit
foreach statement because the query itself must use
foreach in order to return a result. Note also that these
types of queries return a single value, not an
IEnumerable collection.
Forcing Immediate Execution

• Queries that perform aggregation functions over a range


of source elements must first iterate over those
elements. Examples of such queries are Count, Max,
Average, and First. These execute without an explicit
foreach statement because the query itself must use
foreach in order to return a result. Note also that these
types of queries return a single value, not an
IEnumerable collection.
All in One
The Collection Initializer +
Expression Query + Anonymous
Type + Implicit variable +Automatic
Property
Automatic Property
Collection initializer
Query expression
Implicitly Typed Variable.

Anonymous Types
Summary

• As developers we now have a much


more concise way to define objects,
initialize them, and add them to
collections
Lambda Expression
C# 3.0 Language Enhancements for LINQ
To truly understand much of the syntax of LINQ, it is
necessary for me to cover some of the new C# 3.0
language features before proceeding with the workings
of the components of LINQ.

• Lambda expressions
• Expression trees
• The keyword var, object and collection initialization, and
anonymous types
• Extension methods
• Partial methods
• Query expressions
Lambda Expressions

• In C# 3.0, Microsoft has added lambda expressions.

• Lambda Expressions provide a more concise,


functional syntax for writing anonymous methods.

• super useful when writing LINQ query expressions


This method should have nam
Using Lambda Expressions to Create
Expression Trees
• LINQ treats data as objects.

• LINQ provides the ability to treat expressions as data at


runtime.

• LINQ defines a new type, Expression<T>, which represents an


expression tree, an in-memory representation of a lambda
expression.

• Expression trees allow developers to treat lambda expressions as


data, allowing for inspection and modification of lambda expressions
in code.
Relationship between functions and expressions
LINQ provides the ability to treat expressions as data at runtime. LINQ
defines a new type, Expression<T>, which represents an expression tree,
an in-memory representation of a lambda expression.
Actually we converted an expression tree into a delegate that can be executed
It is very easy to create and build expressions that can be compiled
dynamically and create new Func objects that pass around code as objects
Conclusion On expression Tree

a database access implementation might leverage this facility


to translate expression trees into suitable query statements
for a particular kind of database.
No int is required as a result lambda has edge over delegate
The end

You might also like