c# 5.0

11
C # 5.0 New Features By S. M. Ali Raza Naqvi Senior Software Engineer

Upload: ali-raza

Post on 31-Aug-2014

352 views

Category:

Education


5 download

DESCRIPTION

 

TRANSCRIPT

Page 1: C# 5.0

C # 5.0New Features

By S. M. Ali Raza NaqviSenior Software Engineer

Page 2: C# 5.0

Basic introduction to Async and Await keywords

Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute (if we got time)

Agenda

Page 3: C# 5.0

A keyword use to create async methods Async method must contains ‘Await’ keyword Async methods invoke synchronously Await is the point where method suspend

and resume asynchronously Purpose of Await keyword is to wait for the

task to complete

Async & Await

Page 4: C# 5.0

Only methods that return void, Task or Task<Tresult> can mark as Async

Methods which can’t mark async: Main, method implementing synchronized attribute or security

Await can’t be use in methods that are not mark Async Inside Properties Inside lock, catch or finally block

Async & Await (Cont.)

Page 5: C# 5.0

Async & Await - Flow

Output:

Page 6: C# 5.0

Compiler uses state machine to track the current state of the flow

State machine holds the parameters, local variables and the current state of the method

When encounter await, It stores state machine object as variable and resume it when awaiter gets the response

Async & Await - Behind the scene

Page 7: C# 5.0

Readable and maintainable code Allow you to write asynchronous code in

synchronous fashion Exception handling using try..catch block Exceptions with much more details Allow you to use “using” block

Async & Await - Ease

Page 8: C# 5.0

You can create customize awaitable types using IAwaitable interface

You can cancel an existing ongoing task using CancellationTokenSource object

You can deal with multiple task all at once and can use await for all of them at a time

Async & Await - Moreover

Page 9: C# 5.0

Lets see the Code

Page 10: C# 5.0

You can obtain information about the caller to a method

To help in tracing CallerFilePath – return the caller’s file

location CallerLineNumber – return the line number

from where that method called CallerMemberName – return the name of the

method that calls that method

Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and CallerMemberNameAttribute

Page 11: C# 5.0

It don’t involve any reflection

Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and

CallerMemberNameAttribute (Cont.)

At compile time