c# 5.0

Post on 31-Aug-2014

352 Views

Category:

Education

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

C # 5.0New Features

By S. M. Ali Raza NaqviSenior Software Engineer

Basic introduction to Async and Await keywords

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

Agenda

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

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.)

Async & Await - Flow

Output:

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

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

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

Lets see the Code

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

It don’t involve any reflection

Tracing attributes: CallerFilePathAttribute, CallerLineNumberAttribute and

CallerMemberNameAttribute (Cont.)

At compile time

top related