41
Developing Windows and Web Applications using Visual Studio.NET Peter Gfader

C# advanced topics and future - C#5

Embed Size (px)

DESCRIPTION

C# 3.0 Language EnhancementC# 4.0 Language EnhancementC# 5What is LINQ?LINQ Project GoalsLINQ Project ArchitectureData Binding via LINQ

Citation preview

Page 1: C# advanced topics and future - C#5

Developing Windows and Web Applications using Visual Studio.NET

Peter Gfader

Page 2: C# advanced topics and future - C#5

Who did the C# tutorial?

What is .NET?

What is the .NET framework?

What is the CLR?

What is OOP?

Session 1: Last week

Page 3: C# advanced topics and future - C#5

Agenda

C# 3.0 Language Enhancement

C# 4.0 Language Enhancement

C# 5

What is LINQ?

LINQ Project Goals

LINQ Project Architecture

Data Binding via LINQ

Session 2: Overview

Page 4: C# advanced topics and future - C#5

C# 32008

Page 5: C# advanced topics and future - C#5

Local Variable Type Interface

Anonymous Type

Lambda Expressions

Extension methods

Object initializers

Query Expressions

C# 3.0 Language Enhancement

Page 6: C# advanced topics and future - C#5

Local Variable Type Inference

Page 7: C# advanced topics and future - C#5

Fits with complex temporarily variables

E.g. I want a temp variable to hold username & password

Anonymous Type

Page 8: C# advanced topics and future - C#5

Anonymous methods in C#2

NumericalOperation mul1 = (x, y) => x * y;

Console.WriteLine( mul1(4, 5) );

Lambda Expressions

Page 9: C# advanced topics and future - C#5

Extend every class with a static „helper“ method

Extension methods

Page 10: C# advanced topics and future - C#5

More

Object initializer’s

Query Expressions

Please visit:

HookedOnLINQ.com

Page 11: C# advanced topics and future - C#5

C# 4April 2010

Page 12: C# advanced topics and future - C#5

„dynamic“ keyword

Optional parameters

Named parameters

Co and Contra variance

C# 4 Language Enhancement

Page 13: C# advanced topics and future - C#5

Co and Contra varianceHow are generic types “broken” today?

var sheep = new List<Sheep>(); Speak(sheep);

void Speak(IEnumerable<Animal> animals) { // Do something with Animals }

class Animal { } class Sheep : Animal { }

Not Allowed:IEnumerable<Animal> != IEnumerable<Sheep>

Page 14: C# advanced topics and future - C#5

Covariance – think “out”

Contravariance – think “in”

Break it down…

Page 15: C# advanced topics and future - C#5

C# 5????? 2013 ????

Page 16: C# advanced topics and future - C#5

„Meta programming is painful”

Compiler as a service

C# 5 ? - Release date?

Page 17: C# advanced topics and future - C#5

Auto Implemented Properties

Collection Initializers

Named parameters

Optional parameters

Lambdas

Can span statements across multiple lines

Auto Implemented Properties

Collection Initializers (limited)

Named parameters

Optional parameters

Lambdas (limited)

Can span statements across multiple lines

.NET 3.5

Page 18: C# advanced topics and future - C#5

Auto Implemented Properties

Collection Initializers

Named parameters

Optional parameters

Lambdas

Can span statements across multiple lines

Auto Implemented Properties

Collection Initializers

Named parameters

Optional parameters

Lambdas

Can span statements across multiple lines

.NET 4

Page 19: C# advanced topics and future - C#5

Data accessShow me the data!

Page 20: C# advanced topics and future - C#5

The problem

Page 21: C# advanced topics and future - C#5

Problem

Page 22: C# advanced topics and future - C#5

First showcased at the American International Toy Fair in New York in 2005

LINQ, a clever bluffing game

What is LINQ?

Page 23: C# advanced topics and future - C#5

Language INtegrated Query

Allows you to easily query information about an object

LINQ to SQL: An ORM made by Microsoft LINQ to Entities: Another ORM made by Microsoft LINQ to XML: Query over XML ...

What is LINQ?

Page 24: C# advanced topics and future - C#5

Language syntax+ a library of extension methods+ lambda expressions+ anonymous types+ a little magic

What is LINQ?

Page 25: C# advanced topics and future - C#5

.NET Framework 3.5

Page 26: C# advanced topics and future - C#5

LINQ is a natural language extension, part of .NET 3.5

Different data sources are queried with the same syntax.

Queries are strongly typed

Queries benefit from “Intellisense”

Applications becomes less error prone

LINQ - How does it solve the problem?

Page 27: C# advanced topics and future - C#5

Unified programming model for any data type

Database Relational Data XML Files Collections & Arrays

Introduce more declarative syntax

Helps the system find the best execution strategy

LINQ Project Goals

Page 28: C# advanced topics and future - C#5

LINQ Enabled Data Sources

LINQ Enabled ADO.NET

C# VB Others...

.NET Language Integrated Query (LINQ)

LINQ to

Objects

LINQ to Dataset

s

LINQ to

SQL

LINQ to

Entities

LINQ to

XML

LINQ to ??

?

<book> <title>

<author/> <price

/> </title></book>

Objects Database XML

LINQ Project Architecture

Page 29: C# advanced topics and future - C#5

LINQ Query

Page 30: C# advanced topics and future - C#5

string strSQL = “SELECT custID FROM Customers“;

SqlCommand cmd = new SqlCommand(strSQL, cnn);

SqlDataAdapter adAdapter = new SqlDataAdapter(cmd);

DataSet dst = new DataSet();

adAdapter.Fill(dst);

Standard Data Access Code

Page 31: C# advanced topics and future - C#5

NorthwindDataContext db = new NorthwindDataContext();

var customers = from c in db.Customers

select c;

Code by LINQ

Page 32: C# advanced topics and future - C#5

How to use LINQ

Page 33: C# advanced topics and future - C#5

Benefits from LINQ

LINQ data binding

LINQ

Page 34: C# advanced topics and future - C#5

Stored procedures are easier to work with

Relationship editor UI is nicer (more like SQL Server)

Performance

In a recent benchmark LINQ to SQL performed 5 times faster than LINQ to Entities

LINQ 2 SQL

Page 35: C# advanced topics and future - C#5

More refined ORM than LINQ to SQL

Many to Many relationships

Richer support for Inheritance

Validation

Update model from database

Mapping Editor

LINQ 2 Entities

Page 36: C# advanced topics and future - C#5

Do you know why you should choose "LINQ to Entities" over "LINQ to SQL"?

http://www.ssw.com.au/ssw/Standards/Rules/RulesToBetterLINQ.aspx#WhyLINQtoEntitiesNotSQL

What to choose?

Page 37: C# advanced topics and future - C#5

LINQ Pad

http://www.linqpad.net/

Hot Tip

Page 38: C# advanced topics and future - C#5

New features in C# 3.0

New features in C# 4.0

What is LINQ and Why LINQ

LINQ data binding

Summary

Page 39: C# advanced topics and future - C#5

Introduction to LINQ http://msdn.microsoft.com/en-us/library/bb397897.aspx

Getting Started with LINQ in C# http://msdn.microsoft.com/en-us/library/bb397933.aspx

LINQ Term and more details http://www.hookedonlinq.com/

LinqPad http://www.linqpad.net/

Resources

Page 40: C# advanced topics and future - C#5

What's next?

Page 41: C# advanced topics and future - C#5

Thank You!

Gateway Court Suite 10 81 - 91 Military Road Neutral Bay, Sydney NSW 2089 AUSTRALIA

ABN: 21 069 371 900

Phone: + 61 2 9953 3000 Fax: + 61 2 9953 3105

[email protected]