C# 7.0 Hacks and Features

251 views

Published on

The presentation talks about latest version of C# with context to next version of it.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
251
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

C# 7.0 Hacks and Features

  1. 1. C# 7.0What?! C# is Being Updated? Abhishek Sur, Microsoft MVP Architect, Insync
  2. 2. Page HistoryofC# 2
  3. 3. Page C# 2.0 • Generics • Partial types • Anonymous methods • Iterators • Nullable types • Getter/Setter separate accessibility • Static classes • And more.. 3
  4. 4. Page C# 3.0 • Implicitly typed local variables • Object and collection initializers • Auto-properties • Anonymous types • Extension methods • Query expressions • Lambda expressions • Expression trees • And more.. 4
  5. 5. Page C# 4.0 • Dynamic binding • Named and optional parameters • Generic co- and contravariance • And more.. 5
  6. 6. Page C# 5.0 • Asynchronous methods using Async and Await • Caller info attributes 6
  7. 7. Page The state of the Compiler • It’s been a black box • Roslyn to the rescue! • .NET Compiler Platform • Microsoft decides to re-write the compiler • Compiler written in C# • Easier to extend and maintain • Open Source! 7
  8. 8. Page C# 6.0 Overview • Auto-property initializers • Getter-only auto-properties • Assignment to getter-only auto-properties from constructor • Parameter-less struct constructors • Using Statements for Static Members • Dictionary Initializer • Await in catch/finally • Exception filters • Expression-bodied members / Copyright ©2014 by Readify Pty Ltd 8 › Nullpropagation › Stringinterpolation › nameofoperator › Andmore..
  9. 9. Page Auto-Property Initializers 9
  10. 10. Page Auto-property initializers in a nutshell 10 class Person { public string Name { get; set; } } class Person { public string Name { get; } = "Anonymous"; } = "Anonymous";
  11. 11. Page Auto-property initializers in a nutshell 11 class Person { public string Name { get; } public Person() { Name = “Abhishek"; } }
  12. 12. Page Auto-property initializers in a nutshell / Copyright ©2014 by Readify Pty Ltd 12 class Person { private readonly string <Name>k__BackingField = “Abhishek"; public string Name { get { return this.<Name>k__BackingField; } } }
  13. 13. Page Parameter-less structconstructors 13
  14. 14. Page Parameter-less struct constructors in a nutshell / Copyright ©2014 by Readify Pty Ltd 14 @fekberg struct Point { public int X { get; } public int Y { get; } } Read Only! public Point() { X = 100; Y = 100; }
  15. 15. Page Parameter-less struct constructors in a nutshell 15 Read Only! struct Point { private readonly int <X>k__BackingField; private readonly int <Y>k__BackingField; public int X { get { return this.<X>k__BackingField; } } public int Y { get { return this.<Y>k__BackingField; } } public Point() { this.<X>k__BackingField = 100; this.<Y>k__BackingField = 100; } }
  16. 16. Page Parameter-less struct constructors in a nutshell 16 Read Only! struct Point { public int X { get; } public int Y { get; } public Point(int x, int y) { X = x; Y = y; } public Point() : this(100, 100) { } }
  17. 17. Page UsingStatements forStatic Members 17
  18. 18. Page Using Statements for Static Members in a nutshell 18 class Program { static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); } }
  19. 19. Page Using Statements for Static Members in a nutshell 19 using System.Console; using System.Math; class Program { static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); } }
  20. 20. Page Using Statements for Static Members in a nutshell 20 using System.Console; using System.Linq.Enumerable; class Program { static void Main(string[] args) { foreach (var i in Range(0, 10)) { WriteLine(i); } } }
  21. 21. Page Dictionary Initializers
  22. 22. Page Dictionary Initializers in a nutshell 22 var people = new Dictionary<string, Person> { [“abhishek"] = new Person() }; var answers = new Dictionary<int, string> { [42] = "the answer to life the universe and everything" };
  23. 23. Page Awaitinside Finallyblock
  24. 24. Page Await + Finally in a nutshell 24 public async Task DownloadAsync() { } try { } catch { await Task.Delay(2000); } finally { await Task.Delay(2000); }
  25. 25. Page ExceptionFilters
  26. 26. Page Exception filters in a nutshell 26 try { throw new CustomException { Severity = 100 }; } catch (CustomException ex) if (ex.Severity > 50) { Console.WriteLine("*BING BING* WARNING *BING BING*"); } catch (CustomException ex) { Console.WriteLine("Whooops!"); }
  27. 27. Page NullPropagation
  28. 28. Page Null propagation in a nutshell 28 class Person { public string Name { get; set; } public Address Address { get; set; } } Console.WriteLine(abhishek.Address.AddressLine1); var abhishe = new Person { Name = “Abhishek" }; class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } }
  29. 29. Page Null propagation in a nutshell 29 class Person { public string Name { get; set; } public Address Address { get; set; } } Console.WriteLine(abhishek.Address.AddressLine1); var abhishek = new Person { Name = “abhishek" }; class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } }
  30. 30. Page Null propagation in a nutshell 30 class Person { public string Name { get; set; } public Address Address { get; set; } } Console.WriteLine(abhishek.Address.AddressLine1);Console.WriteLine(abhishek.Address == null ? "No Address" : abhishek.Address.AddressLine1); var abhishek = new Person { Name = “abhishek" }; class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } }
  31. 31. Page Null propagation in a nutshell 31 class Person { public string Name { get; set; } public Address Address { get; set; } } Console.WriteLine(ahishek.Address.AddressLine1);Console.WriteLine(abhishek.Address == null ? "No Address" : abhishek.Address.AddressLine1); Console.WriteLine(abhishek?.Address?.AddressLine1 ?? "No Address"); var abhishek = new Person { Name = “abhishek" }; class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } }
  32. 32. Page Null propagation in a nutshell 32 @fekberg var people = new[] { new Person(), null }; WriteLine(people[0]?.Name); WriteLine(people[1]?.Name);
  33. 33. Page Null propagation in a nutshell 33 Person[] people = null; WriteLine(people?[0]?.Name); Person[] people = null; Console.WriteLine( (people != null) ? ((people[0] == null) ? null : people[0].Name) : null );
  34. 34. Page Expression-bodied members
  35. 35. Page Expression-bodied members in a nutshell 35 class Rectangle { public double Width { get; set; } public double Height { get; set; } public double Area => Width * Height; }
  36. 36. Page Expression-bodied members in a nutshell 36 class Rectangle { public double Width { get; set; } public double Height { get; set; } public override string ToString() => "My Width is {Width} and my Height is {Height}"; }
  37. 37. Page String interpolation
  38. 38. Page String interpolation in a nutshell 38 public override string ToString() => "My Width is {Width} and my Height is {Height}"; public override string ToString() => $"My Width is {Width} and my Height is {Height}"; Syntax will change in a later release to the following:
  39. 39. Page String interpolation in a nutshell 39 public override string ToString() => "My Width is {Width} and my Height is {Height}"; public override string ToString() { object[] args = new object[] { this.Width, this.Height }; return string.Format("My Width is {0} and my Height is {1}", args); }
  40. 40. Page String interpolation in a nutshell 40 int age = 28; var result = "Hello there, I'm {age : D5} years old!"; WriteLine(result); int num = 28; object[] objArray1 = new object[] { num }; Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1));
  41. 41. Page nameof operator
  42. 42. Page nameof operator in a nutshell 42 static void Main(string[] args) { WriteLine("Parameter name is: {nameof(args)}"); }
  43. 43. Page nameof operator in a nutshell 43 public double CalculateArea(int width, int height) { if (width <= 0) { throw new ArgumentException("Parameter {nameof(width)} cannot be less than 0"); } return width * height; }
  44. 44. Page There’smore?? C#7.0 44
  45. 45. Page How to try? 45 • InstallVisual Studio Preview 2 and create project with experimental compilation symbol.
  46. 46. Page What about C# 7.0? • Binary literals and Digit separators 46 0b00001000 0xFF_00_FA_AF
  47. 47. Page What about C# 7.0? • Event initializers 47 var client = new WebClient { DownloadFileCompleted += DownloadFileCompletedHandler };
  48. 48. Page What about C# 7.0? • Nested Methods 48 Void MyMethod(){ void calculator(int x) { return x * 2; }; return calculator(10); }
  49. 49. Page What about C# 7.0? • Field targets on auto-properties 49 [field: NonSerialized] public int Age { get; set; }
  50. 50. Page What about C# 7.0? • Inherent use ofTuples and language integration 50 (int x, int y) MyFunction(){ Console.WriteLine(“My Tuple is called”); }
  51. 51. Page What about C# 7.0? Inherent use ofTuples and language integration 51 public class Person { public readonly (string firstName, string lastName) Names; // a tuple public Person((string FirstName, string LastName)) names, int Age) { Names = names; } }
  52. 52. Page What about C# 7.0? Inherent use ofTuples and language integration 52 (string first, string last) = GetNames("Inigo Montoya")
  53. 53. Page What about C# 7.0? • Using params with IEnumerable 53 int Avg(params IEnumerable<int> numbers)
  54. 54. Page What about C# 7.0? • Declaration Expressions 54 public void CalculateAgeBasedOn(int birthYear, out int age) { age = DateTime.Now.Year - birthYear; } CalculateAgeBasedOn(1987, out var age);
  55. 55. Page What about C# 7.0? • Primary Constructors 55 class Person(string name, int age) { private string _name = name; private int _age = age; }
  56. 56. Page What about C# 7.0? • Pattern Matching 56 object obj; // ... switch(obj) { case 42: // ... case Color.Red: // ... case string s: // ... case Point(int x, 42) where (Y > 42): // ... case Point(490, 42): // fine // ... default: // ... }
  57. 57. Page What about C# 7.0? • Async Streams • IAsyncEnumerable will also work withAwait. It has MoveNextAsync and Current property like IEnumerable, and you can call them using foreach loops. 57
  58. 58. Page What about C# 7.0? • Immutable Objects • Inherently thread-safe • Makes it easier to use and reason about code • Easier to parallelize your code • Reference to immutable objects can be cached, as they won’t change 58 public immutable class Point { public Point(int x, int y) { x = x; Y = y; } public int X { get; } public int Y { get; } }
  59. 59. Page Summary • C# 6.0 is awesome • There’s a lot of change in C# 6.0 • .NET Compiler Platform ("Roslyn") makes it easy for Microsoft to improve the language • There’s a lot of interesting features that could go in C# 7.0 60
  60. 60. Page Questions? 61

×