What's New in C# 6?

279b474d14f72e4daa1fc76e6f3c929f?s=47 Greg Shackles
November 18, 2015

What's New in C# 6?

279b474d14f72e4daa1fc76e6f3c929f?s=128

Greg Shackles

November 18, 2015
Tweet

Transcript

  1. None
  2. None
  3. public class Monkey { public Monkey(int id, string name) {

    this.id = id; Name = name; } int id; public int Id { get { return id; } } string name; public string Name { get { return name; } set { name = value; } } } public class MonkeyUtils { public static ArrayList GetMonkeys() { ArrayList monkeys = new ArrayList(); monkeys.Add(new Monkey(1, "Sebastian")); monkeys.Add(new Monkey(2, "Sofia")); monkeys.Add(new Monkey(3, "Sofia")); Monkey monkey = (Monkey)monkeys[2]; monkey.Name = "Henry"; return monkeys; } }
  4. None
  5. None
  6. public partial class Monkey { public Monkey(int? id, string name)

    { Id = id; Name = name; bool hasValue = Id.HasValue; } int? id; public int? Id { get { return id; } private set { id = value; } } string name; public string Name { get { return name; } set { name = value; } public static class MonkeyUtils { public static List<Monkey> GetMonkeys() { List<Monkey> monkeys = new List<Monkey>(); monkeys.Add(new Monkey(1, "Sebastian")); monkeys.Add(new Monkey(2, "Sofia")); monkeys.Add(new Monkey(3, "Sofia")); Monkey monkey = monkeys[2]; monkey.Name = "Henry"; return monkeys; } }
  7. None
  8. public partial class Monkey { public Monkey(int? id) { Id

    = id } public Monkey(int? id, string name) { Id = id; Name = name; } public int? Id { get; private set; } public string Name { get; set; } } public static class MonkeyUtils30 { public static List<Monkey> GetMonkeys() { var monkeys = new List<Monkey> { new Monkey(1) { Name = "Sebastian” }, new Monkey(2) { Name = "Sofia” }, new Monkey(3) { Name = "Sofia” }, }; var monkey = monkeys.First(m => m.Id == 3); monkey.Name = "Henry"; monkeys = monkeys.OrderBy(m => m.Name); return monkeys; } }
  9. None
  10. public partial class Monkey { public Monkey(int? id = 0,

    string name = string.Empty) { Id = id; Name = name; } public int? Id { get; private set; } public string Name { get; set; } } public static class MonkeyUtils { public static List<Monkey> GetMonkeys(bool sort = false) { var monkeys = new List<Monkey> { new Monkey(1) { Name = "Sebastian” }, new Monkey(2) { Name = "Sofia” }, new Monkey(3) { Name = "Sofia” }, }; monkeys.Add(new Monkey(name: "George”)); var monkey = monkeys.First(m => m.Id == 3); monkey.Name = "Henry"; if(sort) monkeys = monkeys.OrderBy(m => m.Name); return monkeys; } public static void Test() { var monkeys1 = GetMonkeys(); var monkeys2 = GetMonkeys(false);//same as above var monkeys3 = GetMonkeys(sort: true); }
  11. None
  12. string GetContentLength(string url) { var request = WebRequest.Create(url); var content=

    new MemoryStream(); //time to block the UI :( using (var response = request.GetResponse()) { using (var responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } } return content.Length.ToString(); }
  13. async Task<string> GetContentLength(string url) { var request = WebRequest.Create(url); var

    content = new MemoryStream(); using (var response = await request.GetResponseAsync()) { using (var responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.Length.ToString(); }
  14. None
  15. None
  16. None
  17. public class Monkey { public Monkey() { } public Monkey(int

    id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; private set; } public string Name { get; set; } public string Location { get; set; } public DayOfWeek MostActiveDay { get { return Id == 0 ? DayOfWeek.Friday : DayOfWeek.Monday; } } public void Print() { Console.WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  18. public class Monkey { public Monkey() { } public Monkey(int

    id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; private set; } public string Name { get; set; } public string Location { get; set; } public DayOfWeek MostActiveDay { get { return Id == 0 ? DayOfWeek.Friday : DayOfWeek.Monday; } } public void Print() { Console.WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  19. public class Monkey { public Monkey() { } public Monkey(int

    id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } public string Name { get; } public string Location { get; set; } public DayOfWeek MostActiveDay { get { return Id == 0 ? DayOfWeek.Friday : DayOfWeek.Monday; } } public void Print() { Console.WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  20. public class Monkey { public Monkey() { } public Monkey(int

    id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay { get { return Id == 0 ? DayOfWeek.Friday : DayOfWeek.Monday; } } public void Print() { Console.WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  21. public class Monkey { public Monkey() { } public Monkey(int

    id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay { get { return Id == 0 ? DayOfWeek.Friday : DayOfWeek.Monday; } } public void Print() { Console.WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}",
  22. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay { get { return Id == 0 ? Friday : Monday; } } public void Print() { WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}",
  23. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay { get { return Id == 0 ? Friday : Monday; } } public void Print() { WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}",
  24. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() { WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  25. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() { WriteLine("We printed some stuff"); } public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); }
  26. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); } } }
  27. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName { get { return string.Format("Monkey {0} lives in {1} with Id of {2}", Name, Location, Id); } } }
  28. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName { get { return $"Monkey {Name} lives in {Location} with Id of {Id}"; } } }
  29. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => return Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName { get { return $"Monkey {Name} lives in {Location} with Id of " + "{(Id == 0 ? \"Bananas\" : Id)}"; } } }
  30. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName => $"Monkey {Name} lives in {Location} with Id of " + "{(Id == 0 ? \"Bananas\" : Id)}"; }
  31. using static System.Console; using static System.DayOfWeek; public class Monkey {

    public Monkey() { } public Monkey(int id, string name, string location) { Id = id; Name = name; Location = location; } public int Id { get; } = 0; public string Name { get; } = "Sofia"; public string Location { get; set; } = "South America"; public DayOfWeek MostActiveDay => Id == 0 ? Friday : Monday; public void Print() => WriteLine("We printed some stuff"); public string DisplayName => $"Monkey {Name} lives in {Location} with Id of " + "{(Id == 0 ? \"Bananas\" : Id)}"; }
  32. None
  33. Resource res = null; try { res = await Resource.OpenAsync(…);

    // You could do this. … } catch(ResourceException e) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
  34. Resource res = null; try { res = await Resource.OpenAsync(…);

    // You could do this. … } catch(ResourceException e) when (e.Code == 0) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
  35. Resource res = null; try { res = await Resource.OpenAsync(…);

    // You could do this. … } catch(ResourceException e) when (myFilter(e)) { await Resource.LogAsync(res, e); // Now you can do this … } finally { if (res != null) await res.CloseAsync(); // … and this. }
  36. private static boolLog(Exception e) { /* log it */ ;

    return false; } … try { … } catch (Exception e) when (Log(e)) { }
  37. using System.Linq; IEnumerable<int> range2 = Enumerable.Range(4, 3); IEnumerable<int> squares =

    range2.Select(x => x * x); foreach (int num in squares) { Console.WriteLine(num); } /* This code produces the following output: 16 25 36 */
  38. using static System.Console; using static System.Linq.Enumerable;

  39. using static System.Console; using static System.Linq.Enumerable; var range = Range(4,

    3); //var squares = Select(range, x => x * x); //Not in scope var squares2 = range.Select(x => x * x); foreach (int num in squares2) { WriteLine(num); }
  40. public static void GetMonkey(int count) { if(count < 0) throw

    new ArgumentNullException("count"); var monkeys = GetMonkeys(); WriteLine("Name: " + monkeys[0].Name); // prints "Name Sofia" }
  41. public static void GetMonkey(int count) { if(count < 0) throw

    new ArgumentNullException(nameof(count)); var monkeys = GetMonkeys(); WriteLine(nameof(monkeys[0].Name) + ": " + monkeys[0].Name); // prints "Name Sofia" }
  42. Insights.Report(exception, new Dictionary <string, string> { {"Some additional info", "foobar"},

    {"Name", "James"} } );
  43. Insights.Report(exception, new Dictionary <string, string> { ["Some additional info"] =

    "foobar", ["Name"] = "James" } );
  44. var numbers = new Dictionary<int, string> { [7] = "seven",

    [9] = "nine", [13] = "thirteen" };
  45. None
  46. public int Rating { set { if (Subviews == null)

    return; var star = UIImage.FromBundle("i_star_rate"); for (int i = 0; i < Subviews.Length; i++) { var imageView = Subviews[i] as UIImageView; if (imageView != null) { imageView.Image = (imageView.Tag <= value) ? star : null; } } } }
  47. public int Rating { set { if (Subviews == null)

    return; var star = UIImage.FromBundle("i_star_rate"); for (int i = 0; i < Subviews.Length; i++) { var imageView = Subviews[i] as UIImageView; imageView?.Image = (imageView.Tag <= value) ? star : null; } } }
  48. public int Rating { set { var star = UIImage.FromBundle("i_star_rate");

    var length = Subviews?.Length; // null var length2 = Subviews?.Length ?? 0; //if length nullable for (int i = 0; i < length; i++) { var imageView = Subviews[i] as UIImageView; imageView?.Image = (imageView.Tag <= value) ? star : null; } } }
  49. public void OnPropertyChanged(string name) { var changed = PropertyChanged; if

    (changed == null) return; changed(this, new PropertyChangedEventArgs(name)); }
  50. public void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); }

  51. http://bit.ly/csharp7-proposals

  52. Thank you. Questions?