Upgrade to Pro — share decks privately, control downloads, hide ads and more …

What's New in C# 6?

Greg Shackles
November 18, 2015

What's New in C# 6?

Greg Shackles

November 18, 2015
Tweet

More Decks by Greg Shackles

Other Decks in Technology

Transcript

  1. 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; } }
  2. 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; } }
  3. 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; } }
  4. 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); }
  5. 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(); }
  6. 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(); }
  7. 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); }
  8. 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); }
  9. 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); }
  10. 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); }
  11. 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}",
  12. 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}",
  13. 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}",
  14. 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); }
  15. 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); }
  16. 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); } } }
  17. 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); } } }
  18. 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}"; } } }
  19. 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)}"; } } }
  20. 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)}"; }
  21. 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)}"; }
  22. 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. }
  23. 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. }
  24. 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. }
  25. private static boolLog(Exception e) { /* log it */ ;

    return false; } … try { … } catch (Exception e) when (Log(e)) { }
  26. 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 */
  27. 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); }
  28. public static void GetMonkey(int count) { if(count < 0) throw

    new ArgumentNullException("count"); var monkeys = GetMonkeys(); WriteLine("Name: " + monkeys[0].Name); // prints "Name Sofia" }
  29. 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" }
  30. 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; } } } }
  31. 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; } } }
  32. 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; } } }
  33. public void OnPropertyChanged(string name) { var changed = PropertyChanged; if

    (changed == null) return; changed(this, new PropertyChangedEventArgs(name)); }