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# 7.0, 7.1, 7.2

731 views

Published on

features of C# 7.0, 7.1, 7.2 using visual studio 2017

Published in: Software
  • Be the first to comment

  • Be the first to like this

C# 7.0, 7.1, 7.2

  1. 1. Features CSharp 7, 7.1 y 7.2 @mteheran - Miguel Teheran
  2. 2. Who am I?
  3. 3. CSharp - 2000 - C++, Java, Pascal - Microsoft - .Net Framework - Mono and Unity - OOP
  4. 4. 7.0 Demos
  5. 5. Out variables int numericResult; if (int.TryParse(input, out numericResult)) WriteLine(numericResult); else WriteLine("Could not parse input"); Now if (int.TryParse(input, out int result)) WriteLine(result); else WriteLine("Could not parse input");
  6. 6. Local functions static void Main(string[] args) { int Sum(int x, int y) { return x + y; } Console.WriteLine(Sum(10, 20)); Console.ReadKey(); }
  7. 7. Ref locals and returns public ref Person GetContactInformation(string fname, string lname); return ref p; ref Person p = ref contacts.GetContactInformation("Brandie", "Best");
  8. 8. Digital Separator public const long BillionsAndBillions = 100_000_000_000;
  9. 9. Pattern matching public static double ComputeArea(object shape) { switch (shape) { case Square s when s.Side == 0: case Circle c when c.Radius == 0: return 0; case Square s: return s.Side * s.Side; case Circle c: return c.Radius * c.Radius * Math.PI; default: throw new ArgumentException( message: "shape is not a recognized shape", paramName: nameof(shape)); } }
  10. 10. Tuples var named = (first: "one", second: "two"); //method returning a tuple (bool bresult, double dcalculation) = Tuples.CalculationTuple();
  11. 11. Throw expressions loadedConfig = LoadConfigResourceOrDefault(); if (loadedConfig == null) throw new InvalidOperationException("Could not load config"); Now private ConfigResource loadedConfig = LoadConfigResourceOrDefault() ?? throw new InvalidOperationException("Could not load config");
  12. 12. 7.1 Demos
  13. 13. Tuple element names int count = 5; string label = "Colors used in the map"; var pair = (count: count, label: label); Now int count = 5; string label = "Colors used in the map"; var pair = (count, label); // element names are "count" and "label"
  14. 14. Async main static int Main() { return DoAsyncWork().GetAwaiter().GetResult(); } Now static async Task<int> Main() { // This could also be replaced with the body // DoAsyncWork, including its await expressions: return await DoAsyncWork(); }
  15. 15. 7.2 is coming soon
  16. 16. Private Protected public class Employee { public int Id { get; set; } protected int Name { get; set; } internal DateTime DOB { get; set; } private Decimal Salary { get; set; } protected internal int DepatmentId { get; set; } private protected string ProjectName { get; set; } } class SubEmployee : Employee { void Print() { SubEmployee employee = new SubEmployee(); WriteLine(employee.ProjectName); } }
  17. 17. Private Protected private protected member is inaccessible inside the assembly for non-inherited class.
  18. 18. Reference semantics with value types ● The in modifier on parameters, to specify that an argument is passed by reference but not modified by the called method. ● The ref readonly modifier on method returns, to indicate that a method returns its value by reference but doesn't allow writes to that object. ● The readonly struct declaration, to indicate that a struct is immutable and should be passed as an inparameter to its member methods. ● The ref struct declaration, to indicate that a struct type accesses managed memory directly and must always be stack allocated.
  19. 19. 8.0 the future
  20. 20. Nullable reference types public class Person { public string FirstName { get; set; } public string? MiddleName { get; set; } public string LastName { get; set; } } void M(Person p) { WriteLine(p.MiddleName.Length); // WARNING: may be null WriteLine(p.MiddleName?.Length); // ok, you know best! }
  21. 21. https://github.com/dotnet/csharplang The official repo for the design of the C# programming language
  22. 22. DEMOS https://github.com/Mteheran/CSharpVersionsDemos https://mteheran.wordpress.com/
  23. 23. Questions??? Thanks

×