What’s New In C# 5.0Paulo Morgado
Paulo MorgadoCodePlexRevistaPROGRAMAR
A Language For Each Generation
The Evolution Of C#C# 1.0 C# 2.0 C# 3.0 C# 4.0 C# 5.0Managed Generics LINQ Dynamic Async
The Evolution Of C#C# 1.0 C# 2.0 C# 3.0 C# 4.0 C# 5.0Managed Generics LINQ Dynamic Asyncplease wait for the next slideclic...
DemoSynchronous UI Application
Synchronous UI Applicationprivate voidHandleLoadButtonClick(objectsender, EventArgs e){try{this.loadButton.Visible =false;...
Introducing Async - YesterdayClickvoid LoadImage(){// ...LoadLocalData(...);// ...}void Button_Click(...){LoadImage();Upda...
Introducing Async - TodayClickvoid LoadImage(){// ...DownloadRemoteData(...);// ...}void Button_Click(...){LoadImage();Upd...
DemoAdd the async & await keywords
Add the async & await keywordsprivate async voidHandleLoadButtonClick(object sender,EventArgs e){try{this.loadButton.Visib...
Introducing Asyncasync void Button_Click(...){await LoadImageAsync();UpdateView();}async Task LoadImageAsync(){// ...await...
Introducing AsyncClickasync Task LoadImageAsync(){// ...await DownloadRemoteDataAsync(...);// ...}async void Button_Click(...
DemoAsync UI app: re-entrancy and deadlock
Async UI app: deadlockprivate voidHandleLoadButtonClick(object sender,EventArgs e){try{this.loadButton.Visible =false;this...
DemoAsync with cancelation
Async with cancelationprivate async void HandleLoadButtonClick(objectsender, EventArgs e){try{this.loadButton.Visible = fa...
DemoAsync console app
Async console appclass Program{static void Main(string[] args){Run().Wait();}static async Task Run(){await Task.Yield();}}
DemoAsync unit tests
Async unit tests[TestMethod]public async Task TestMethod1(){await Task.Delay(5000);Assert.Fail();}
Source Code Caller ID
Source Code Caller ID• CallerFilePathAttribute– Allows you to obtain the full path of the source file that contains the ca...
Source Code Caller ID - Examplesstatic void TraceMessage(string message,[CallerMemberName] string memberName = "",[CallerF...
Source Code Caller ID - Examplesprivate string field;public string Property{get { return this.field; }set{if (this.field !...
Breaking Changes
Breaking Changes• You can use the iteration variable of a foreach statement in a lambdaexpression that’s contained in the ...
Resources
Resources• C# Reference– http://msdn.microsoft.com/library/618ayhy6.aspx• Breaking Changes in C# 5.0– http://msdn.microsof...
Resources• Task-based Asynchronous Pattern– http://msdn.microsoft.com/library/hh191443.aspx• Task.Run vs Task.Factory.Star...
Resources• Lucian Wischiks Blog– http://blogs.msdn.com/b/lucian/archive/tags/async/• Parallel Programming Team Blog– http:...
Resources• Paulo Morgado– @PauloMorgado– http://PauloMorgado.NET/– http://mvp.support.microsoft.com/profile/Paulo.Morgado–...
Q & A
Thank You!
Upcoming SlideShare
Loading in …5
×

Paulo morgado what's new in c# 5.0

1,057 views

Published on

Published in: Technology, Art & Photos
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,057
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Paulo morgado what's new in c# 5.0

  1. 1. What’s New In C# 5.0Paulo Morgado
  2. 2. Paulo MorgadoCodePlexRevistaPROGRAMAR
  3. 3. A Language For Each Generation
  4. 4. The Evolution Of C#C# 1.0 C# 2.0 C# 3.0 C# 4.0 C# 5.0Managed Generics LINQ Dynamic Async
  5. 5. The Evolution Of C#C# 1.0 C# 2.0 C# 3.0 C# 4.0 C# 5.0Managed Generics LINQ Dynamic Asyncplease wait for the next slideclicking won’t make it come any faster
  6. 6. DemoSynchronous UI Application
  7. 7. Synchronous UI Applicationprivate voidHandleLoadButtonClick(objectsender, EventArgs e){try{this.loadButton.Visible =false;this.progressBar.Visible =true;this.pictureBox.Image =null;var image =this.LoadImage();this.pictureBox.Image =image;}catch (Exception ex){// ...}finally{this.loadButton.Visible =true;this.progressBar.Visible =false;}}private Image LoadImage(){var imageBytes = newWebClient().DownloadData(Settings.Default.ImageUrl);var image =Bitmap.FromStream(newMemoryStream(imageBytes));return image;}
  8. 8. Introducing Async - YesterdayClickvoid LoadImage(){// ...LoadLocalData(...);// ...}void Button_Click(...){LoadImage();UpdateView();}ClickMessagepump
  9. 9. Introducing Async - TodayClickvoid LoadImage(){// ...DownloadRemoteData(...);// ...}void Button_Click(...){LoadImage();UpdateView();}ClickMessagepump
  10. 10. DemoAdd the async & await keywords
  11. 11. Add the async & await keywordsprivate async voidHandleLoadButtonClick(object sender,EventArgs e){try{this.loadButton.Visible = false;this.progressBar.Visible = true;this.pictureBox.Image = null;var image = awaitthis.LoadImageAsync();this.pictureBox.Image = image;}catch (Exception ex){// ...}finally{this.loadButton.Visible = true;this.progressBar.Visible = false;}}private async Task<Image> LoadImageAsync(){var imageBytes = await new WebClient().DownloadDataTaskAsync(Settings.Default.ImageUrl);var image = Bitmap.FromStream(newMemoryStream(imageBytes));return image;}
  12. 12. Introducing Asyncasync void Button_Click(...){await LoadImageAsync();UpdateView();}async Task LoadImageAsync(){// ...await DownloadRemoteDataAsync(...);// ...}Messagepumpvoid LoadImage(){// ...DownloadRemoteData(...);// ...}void Button_Click(...){LoadImage();UpdateView();}Click
  13. 13. Introducing AsyncClickasync Task LoadImageAsync(){// ...await DownloadRemoteDataAsync(...);// ...}async void Button_Click(...){await LoadImageAsync();UpdateView();}ClickMessagepumpTask ...DownloadRemoteDataAsyncTask ...LoadImageAsyncDownloadLoadImage
  14. 14. DemoAsync UI app: re-entrancy and deadlock
  15. 15. Async UI app: deadlockprivate voidHandleLoadButtonClick(object sender,EventArgs e){try{this.loadButton.Visible =false;this.progressBar.Visible =true;this.pictureBox.Image = null;var image =this.LoadImageAsync().Result;this.pictureBox.Image = image;}catch (Exception ex){// ...}finally{this.loadButton.Visible = true;this.progressBar.Visible =false;}}private async Task<Image>LoadImageAsync(){var imageBytes = await newWebClient().DownloadDataTaskAsync(Settings.Default.ImageUrl);var image = Bitmap.FromStream(newMemoryStream(imageBytes));return image;}
  16. 16. DemoAsync with cancelation
  17. 17. Async with cancelationprivate async void HandleLoadButtonClick(objectsender, EventArgs e){try{this.loadButton.Visible = false;this.progressBar.Visible = true;this.cancelButton.Visible = true;this.pictureBox.Image = null;var a = SynchronizationContext.Current;var image = await this.LoadImageAsync();var b = SynchronizationContext.Current;this.pictureBox.Image = image;}catch (Exception ex){// ...}finally{this.loadButton.Visible = true;this.progressBar.Visible = false;this.cancelButton.Visible = false;}}private async Task<Image> LoadImageAsync(){Image image = null;var a = SynchronizationContext.Current;try{this.cancelationTokenSource = newCancellationTokenSource();var imageResponse = await newHttpClient().GetAsync(Settings.Default.ImageUrl,cancelationTokenSource.Token).ConfigureAwait(false);var x = SynchronizationContext.Current;if (imageResponse != null){var imageStream = awaitimageResponse.Content.ReadAsStreamAsync();var c =SynchronizationContext.Current;image =Bitmap.FromStream(imageStream);}}catch (TaskCanceledException ex){}var b = SynchronizationContext.Current;return image;}private CancellationTokenSourcecancelationTokenSource;private void HandleCancelButtonClick(objectsender, EventArgs e){if (this.cancelationTokenSource != null){this.cancelationTokenSource.Cancel();this.cancelationTokenSource.Dispose();this.cancelationTokenSource = null;}}
  18. 18. DemoAsync console app
  19. 19. Async console appclass Program{static void Main(string[] args){Run().Wait();}static async Task Run(){await Task.Yield();}}
  20. 20. DemoAsync unit tests
  21. 21. Async unit tests[TestMethod]public async Task TestMethod1(){await Task.Delay(5000);Assert.Fail();}
  22. 22. Source Code Caller ID
  23. 23. Source Code Caller ID• CallerFilePathAttribute– Allows you to obtain the full path of the source file that contains the caller.This is the file path at the time of compile.• http://msdn.microsoft.com/library/system.runtime.compilerservices.callerfilepathattribute.aspx• CallerLineNumberAttribute– Allows you to obtain the line number in the source file at which themethod is called.• http://msdn.microsoft.com/library/system.runtime.compilerservices.callerlinenumberattribute.aspx• CallerMemberNameAttribute– Allows you to obtain the method or property name of the caller to themethod.• http://msdn.microsoft.com/library/system.runtime.compilerservices.callermembernameattribute.aspx
  24. 24. Source Code Caller ID - Examplesstatic void TraceMessage(string message,[CallerMemberName] string memberName = "",[CallerFilePath] string sourceFilePath = "",[CallerLineNumber] int sourceLineNumber = 0){Trace.WriteLine(string.Format("{0} at {1} in {2}:line {3}",message,memberName,sourceFilePath,sourceLineNumber));}
  25. 25. Source Code Caller ID - Examplesprivate string field;public string Property{get { return this.field; }set{if (this.field != value){this.field = value;this.NotifyPropertyChanged();}}}protected void NotifyPropertyChanged([CallerMemberName] string propertyName = ""){// …}
  26. 26. Breaking Changes
  27. 27. Breaking Changes• You can use the iteration variable of a foreach statement in a lambdaexpression that’s contained in the body of the loop.• You can use the iteration variable of a foreach statement in a LINQexpression that’s contained in the body of the loop.• Overload resolution has been improved for calls that use namedarguments to access methods that contain params parameters.• Overload resolution is improved for calls where the algorithm mustchoose between a Func<object> parameter and a Func parameter thathas a different type parameter (e.g., string or int?) for aFunc<dynamic> argument.• Side effects from named and positional arguments in a method callnow occur from left to right in the argument list.http://msdn.microsoft.com/library/hh678682(v=vs.110).aspx
  28. 28. Resources
  29. 29. Resources• C# Reference– http://msdn.microsoft.com/library/618ayhy6.aspx• Breaking Changes in C# 5.0– http://msdn.microsoft.com/library/hh678682(v=vs.110).aspx• .NET Framework 4.5– http://msdn.microsoft.com/library/vstudio/w0x726c2(v=vs.110).aspx• Task Parallel Library (TPL)– http://msdn.microsoft.com/library/vstudio/dd460717.aspx• Asynchronous Programming with Async and Await (C# and VisualBasic)– http://msdn.microsoft.com/library/hh191443.aspx
  30. 30. Resources• Task-based Asynchronous Pattern– http://msdn.microsoft.com/library/hh191443.aspx• Task.Run vs Task.Factory.StartNew– http://blogs.msdn.com/b/pfxteam/archive/2011/10/24/10229468.aspx• An Async Premier– http://msdn.microsoft.com/vstudio/jj573641.aspx• Eduasync by Jon Skeet– http://msmvps.com/blogs/jon_skeet/archive/tags/Eduasync/default.aspx• Eric Lipperts Blog– http://ericlippert.com/– http://blogs.msdn.com/b/ericlippert/archive/tags/c_2300_+5-0/async/
  31. 31. Resources• Lucian Wischiks Blog– http://blogs.msdn.com/b/lucian/archive/tags/async/• Parallel Programming Team Blog– http://blogs.msdn.com/b/pfxteam/archive/tags/async/• What’s new in C#5? – Red Gate– http://www.youtube.com/watch?v=z7nry67oeKc• Novidades Do C# 5.0 – Comunidade NetPonto– http://www.youtube.com/watch?v=7Tl6CHf86z4• Sample Code– http://code.msdn.microsoft.com/C-50-AsyncAwait-Demo-Code-334679a5
  32. 32. Resources• Paulo Morgado– @PauloMorgado– http://PauloMorgado.NET/– http://mvp.support.microsoft.com/profile/Paulo.Morgado– http://msmvps.com/blogs/paulomorgado/– http://weblogs.asp.net/paulomorgado/– http://pontonetpt.org/blogs/paulomorgado/– http://www.codeproject.com/Members/PauloMorgado– http://code.msdn.microsoft.com/site/search?f%5B0%5D.Type=User&f%5B0%5D.Value=Paulo%20Morgado– http://www.codeplex.com/UserAccount/UserProfile.aspx?UserName=PauloMorgado– http://www.slideshare.net/PauloJorgeMorgado
  33. 33. Q & A
  34. 34. Thank You!

×