Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

C# 6.0 - What?! C# is being updated?

11,236 views

Published on

Did you think C# was already the perfect programming language? Did you think there was no work left on refining the language?

In this session I’ll walk through what’s new in C# 6.0, how the new open-source compiler helped and why we have an awesome time ahead!

  • Login to see the comments

C# 6.0 - What?! C# is being updated?

  1. 1. C# 6.0 What?! C# is Being Updated? Filip Ekberg
  2. 2. Page / Copyright ©2014 2 by Readify Pty Ltd @fekberg
  3. 3. Page Filip Ekberg C# MVP http://fekberg.com @fekberg C# Smorgasbord / Copyright ©2014 3 by Readify Pty Ltd
  4. 4. http://en.wikipedia.org/wiki/C_Sharp_(programming_language)#History Page History of C# / Copyright ©2014 4 by Readify Pty Ltd
  5. 5. Page C# 2.0 › Generics › Partial types › Anonymous methods › Iterators › Nullable types › Getter/Setter separate accessibility › Static classes › And more.. / Copyright ©2014 5 by Readify Pty Ltd @fekberg
  6. 6. 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.. / Copyright ©2014 6 by Readify Pty Ltd @fekberg
  7. 7. Page C# 4.0 › Dynamic binding › Named and optional parameters › Generic co-and contravariance › And more.. / Copyright ©2014 7 by Readify Pty Ltd @fekberg
  8. 8. Page C# 5.0 › Asynchronous methods › Caller info attributes / Copyright ©2014 8 by Readify Pty Ltd @fekberg
  9. 9. 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! / Copyright ©2014 9 by Readify Pty Ltd @fekberg
  10. 10. 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 10 by Readify Pty Ltd › Null propagation › String interpolation › nameofoperator › And more..
  11. 11. Page Auto-Property Initializers / Copyright ©2014 11 by Readify Pty Ltd
  12. 12. Page Auto-property initializers in a nutshell class Person { public string Name { get; set; } } class Person { = "Anonymous"; public string Name { get; } = "Anonymous"; } / Copyright ©2014 12 by Readify Pty Ltd @fekberg
  13. 13. Page Auto-property initializers in a nutshell class Person { public string Name { get; } public Person() { Name = "Filip"; } } / Copyright ©2014 13 by Readify Pty Ltd @fekberg
  14. 14. Page Auto-property initializers in a nutshell class Person { private readonly string <Name>k__BackingField = "Filip"; public string Name { get { return this.<Name>k__BackingField; } } } / Copyright ©2014 14 by Readify Pty Ltd @fekberg
  15. 15. Page Parameter-less struct constructors / Copyright ©2014 15 by Readify Pty Ltd
  16. 16. Page Parameter-less struct constructors in a nutshell struct Point { public int X { get; } public int Y { get; } } Read Only! public Point() { X = 100; Y = 100; } / Copyright ©2014 16 by Readify Pty Ltd @fekberg
  17. 17. Page Parameter-less struct constructors in a nutshell 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; } } / Copyright ©2014 17 by Readify Pty Ltd @fekberg
  18. 18. Page Parameter-less struct constructors in a nutshell 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) { } } / Copyright ©2014 18 by Readify Pty Ltd @fekberg
  19. 19. Page Using Statements for Static Members / Copyright ©2014 19 by Readify Pty Ltd
  20. 20. Page Using Statements for Static Members in a nutshell class Program { static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); } } / Copyright ©2014 20 by Readify Pty Ltd @fekberg
  21. 21. Page Using Statements for Static Members in a nutshell using System.Console; using System.Math; class Program { static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); } } / Copyright ©2014 21 by Readify Pty Ltd @fekberg
  22. 22. Page Using Statements for Static Members in a nutshell using System.Console; using System.Linq.Enumerable; class Program { static void Main(string[] args) { foreach (var i in Range(0, 10)) { WriteLine(i); } } } / Copyright ©2014 22 by Readify Pty Ltd @fekberg
  23. 23. Page Dictionary Initializers / Copyright ©2014 23 by Readify Pty Ltd
  24. 24. Page Dictionary Initializers in a nutshell var people = new Dictionary<string, Person> { ["Filip"] = new Person() }; var answers = new Dictionary<int, string> { [42] = "the answer to life the universe and everything" }; / Copyright ©2014 24 by Readify Pty Ltd @fekberg
  25. 25. Page Await inside Finally block / Copyright ©2014 25 by Readify Pty Ltd
  26. 26. Page Await + Finally in a nutshell public async Task DownloadAsync() { } try { } catch { await Task.Delay(2000); } finally { await Task.Delay(2000); } / Copyright ©2014 26 by Readify Pty Ltd @fekberg
  27. 27. Page Exception Filters / Copyright ©2014 27 by Readify Pty Ltd
  28. 28. Page Exception filters in a nutshell 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!"); } / Copyright ©2014 28 by Readify Pty Ltd @fekberg
  29. 29. Page Null Propagation / Copyright ©2014 29 by Readify Pty Ltd
  30. 30. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address.AddressLine1); / Copyright ©2014 30 by Readify Pty Ltd @fekberg
  31. 31. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address.AddressLine1); / Copyright ©2014 31 by Readify Pty Ltd @fekberg
  32. 32. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); / Copyright ©2014 32 by Readify Pty Ltd @fekberg
  33. 33. Page Null propagation in a nutshell class Person { public string Name { get; set; } public Address Address { get; set; } } class Address { public string AddressLine1 { get; set; } public string AddressLine2 { get; set; } } var filip = new Person { Name = "Filip" }; Console.WriteLine(filip.Address .=A=d dnruelslsL?i n"eN1o) ;Address" : filip.Address.AddressLine1); Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address"); / Copyright ©2014 33 by Readify Pty Ltd @fekberg
  34. 34. Page Null propagation in a nutshell var people = new[] { new Person(), null }; WriteLine(people[0]?.Name); WriteLine(people[1]?.Name); / Copyright ©2014 34 by Readify Pty Ltd @fekberg
  35. 35. Page Null propagation in a nutshell Person[] people = null; WriteLine(people?[0]?.Name); Person[] people = null; Console.WriteLine( (people != null) ? ((people[0] == null) ? null : people[0].Name) : null ); / Copyright ©2014 35 by Readify Pty Ltd @fekberg
  36. 36. Page Expression-bodied members / Copyright ©2014 36 by Readify Pty Ltd
  37. 37. Page Expression-bodied members in a nutshell class Rectangle { public double Width { get; set; } public double Height { get; set; } public double Area => Width * Height; } / Copyright ©2014 37 by Readify Pty Ltd @fekberg
  38. 38. Page Expression-bodied members in a nutshell 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}"; } / Copyright ©2014 38 by Readify Pty Ltd @fekberg
  39. 39. Page String interpolation / Copyright ©2014 39 by Readify Pty Ltd
  40. 40. Page String interpolation in a nutshell public override string ToString() => "My Width is {Width} and my Height is {Height}"; Syntax will change in a later release to the following: public override string ToString() => $"My Width is {Width} and my Height is {Height}"; / Copyright ©2014 40 by Readify Pty Ltd @fekberg
  41. 41. Page String interpolation in a nutshell 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); } / Copyright ©2014 41 by Readify Pty Ltd @fekberg
  42. 42. int num = 28; object[] objArray1 = new object[] { num }; Console.WriteLine(string.Format("Hello there, I'm {0:D5} years old!", objArray1)); Page String interpolation in a nutshell int age = 28; var result = "Hello there, I'm {age : D5} years old!"; WriteLine(result); / Copyright ©2014 42 by Readify Pty Ltd @fekberg
  43. 43. Page nameof operator / Copyright ©2014 43 by Readify Pty Ltd
  44. 44. Page nameof operator in a nutshell static void Main(string[] args) { WriteLine("Parameter name is: {nameof(args)}"); } / Copyright ©2014 44 by Readify Pty Ltd @fekberg
  45. 45. Page nameof operator in a nutshell public double CalculateArea(int width, int height) { if (width <= 0) { throw new ArgumentException("Parameter {nameof(width)} cannot be less than 0"); } return width * height; } / Copyright ©2014 45 by Readify Pty Ltd @fekberg
  46. 46. Page Demo C# 6.0 Language Features in Visual Studio 2015 / Copyright ©2014 46 by Readify Pty Ltd
  47. 47. Page There’s more?? / Copyright ©2014 47 by Readify Pty Ltd
  48. 48. Page What about C# 7.0? › Binary literals and Digit separators / Copyright ©2014 48 by Readify Pty Ltd 0b00001000 0xFF_00_FA_AF
  49. 49. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers / Copyright ©2014 49 by Readify Pty Ltd var client = new WebClient { DownloadFileCompleted += DownloadFileCompletedHandler };
  50. 50. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties / Copyright ©2014 50 by Readify Pty Ltd [field: NonSerialized] public int Age { get; set; }
  51. 51. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties › Semicolon operator / Copyright ©2014 51 by Readify Pty Ltd var y = (var x = Foo(); Write(x); x * x);
  52. 52. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties › Semicolon operator › Using params with IEnumerable / Copyright ©2014 52 by Readify Pty Ltd int Avg(params IEnumerable<int> numbers)
  53. 53. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties › Semicolon operator › Using params with IEnumerable › Declaration Expressions / Copyright ©2014 53 by Readify Pty Ltd public void CalculateAgeBasedOn(int birthYear, out int age) { age = DateTime.Now.Year - birthYear; } CalculateAgeBasedOn(1987, out var age);
  54. 54. Page What about C# 7.0? › Binary literals and Digit separators › Event initializers › Field targets on auto-properties › Semicolon operator › Using params with IEnumerable › Declaration Expressions › Primary Constructors › And possibly more... / Copyright ©2014 54 by Readify Pty Ltd class Person(string name, int age) { private string _name = name; private int _age = age; }
  55. 55. Page More with Roslyn › Exposes the CompilerAPIs › Plugins powered by Roslyn › Analyze code › Re-write code / Copyright ©2014 55 by Readify Pty Ltd @fekberg
  56. 56. 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 / Copyright ©2014 56 by Readify Pty Ltd @fekberg
  57. 57. Page Questions? / Copyright ©2014 57 by Readify Pty Ltd
  58. 58. Page Thank you! http://fekberg.com @fekberg C# Smorgasbord / Copyright ©2014 58 by Readify Pty Ltd

×