Last active
May 23, 2016 22:57
-
-
Save JerryNixon/6d87379a3699d584b1d760ea8454b00c to your computer and use it in GitHub Desktop.
Prep for presentation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// C# 6 return complex types | |
public class result | |
{ | |
public int Sum {get;set;} | |
public int Count {get;set;} | |
} | |
public result Total(int[] values) | |
{ | |
return new result | |
{ | |
Sum = values.Sum(), | |
Count = values.Count(), | |
}; | |
} | |
var list = new[] {1, 2, 3, 4, 5}; | |
var total = Total(list); | |
result total = Total(list); | |
Console.WriteLine($"{Sum {total.Sum} Count {total.Count}"); | |
// C# 7 return tuple types | |
public (int sum, int count) Total(int[] values) | |
{ | |
var s = values.Sum(); | |
var c = values.Count(); | |
return (s, c); | |
} | |
var list = new[] {1, 2, 3, 4, 5}; | |
var total = Total(list); | |
(var sum, var count) total = Total(list); | |
(int sum, int count) total = Total(list); | |
Console.WriteLine($"{Sum {total.Sum} Count {total.Count}"); | |
// C# 6 finding patterns | |
int? x = -3; | |
if (x.HasValue) | |
{ | |
var value = x.Value; | |
x = Math.Abs(x); | |
Console.WriteLine($"Value = {value}"); | |
} | |
else | |
{ | |
Console.WriteLine($"Value is null."); | |
} | |
// C# 7 pattern matching | |
int? x = 3; | |
if (x is int v) | |
{ | |
v = Math.Abs(v); | |
Console.WriteLine($"Value = {v}}"); | |
} | |
else | |
{ | |
Console.WriteLine($"Value is null."); | |
} | |
// C# 6 finding patterns | |
var p = new Point(5, 10); | |
if (p.X == 5) | |
{ | |
var y = p.Y; | |
y = Math.Abs(y); | |
Console.Write($"Y = {y}"); | |
} | |
// C# 7 positional & matching expressions | |
var p = new Point(5, 10); | |
if (p is Point { x is 5, y is var y }) | |
{ | |
y = Math.Abs(y); | |
Console.Write($"Y = {y}"); | |
} | |
// C# 6 complex if statements | |
if (o is int) | |
{ | |
var i = Math.Abs(o); | |
Console.WriteLine($"{i} is int") | |
} | |
else if (o is Point) | |
{ | |
var x = Math.Abs(o.X); | |
var y = Math.Abs(o.Y); | |
Console.WriteLine($"Point {x} {y}"); | |
} | |
else if (o is string) | |
{ | |
if (o.Length > 2) | |
{ | |
var s = o.ToLower(); | |
Console.WriteLine($"{s} is {s.Length}"); | |
} | |
} | |
// C# 7 pattern-matching switch | |
switch (o) | |
{ | |
case int i: | |
i = Math.Abs(i); | |
Console.WriteLine($"{i} is int"); | |
break; | |
case Point(int x, int y): | |
x = Math.Abs(x); | |
y = Math.Abs(y); | |
Console.WriteLine($"Point {x} {y}"); | |
Break; | |
case string s when (s.Length > 0): | |
s = s.ToLower(); | |
Console.WriteLine($"{s} is string"); | |
} | |
// C# 6 comparable types | |
public class Person: IEquatable<Person> | |
{ | |
public int Id { get; set; } | |
public string First { get; set; } | |
public string Last { get; set; } | |
public bool Equals<T>(T person) | |
{ | |
if (person?.First != First) | |
{ | |
return false; | |
} | |
else if (person?.Last != Last) | |
{ | |
return false; | |
} | |
return true; | |
} | |
} | |
// C# 7 (Comparable) Records | |
public class Person(string First, string Last); | |
// C# 6 Peer functions | |
public void Encrypter(ref User user) | |
{ | |
user.First = Encrypt(ref user.First); | |
user.Middle = Encrypt(ref user.Middle); | |
user.Last = Encrypt(ref user.Last); | |
} | |
public string Encrypt(ref string value) | |
{ | |
// TODO | |
} | |
// C# 7 Local functions | |
public void Encrypter(ref User user) | |
{ | |
string Encrypt(ref string value) | |
{ | |
// TODO | |
} | |
user.First = Encrypt(ref user.First); | |
user.Middle = Encrypt(ref user.Middle); | |
user.Last = Encrypt(ref user.Last); | |
} | |
// C# 6 memory (problem) | |
public T Choose<T>(ref T left, ref T right) | |
{ | |
return Condition() ? left : right; | |
} | |
// new memory pointer | |
var path = Choose(path1, path2); | |
// C# 7 return ref | |
public ref T Choose<T>(ref T left, ref T right) | |
{ | |
return Condition() ? ref left : ref right; | |
} | |
// same memory pointer | |
var path = Choose(path1, path2); | |
// C# 6 partial methods | |
public partial class User // generated class | |
{ | |
public partial void Save(); | |
} | |
public partial class User | |
{ | |
public partial void Save() | |
{ | |
// TODO: | |
} | |
public async Task SaveAsync() | |
{ | |
// TODO | |
} | |
} | |
// C# 7 supersede modifier | |
public partial class User // generated class | |
{ | |
public void Save() | |
{ | |
// TODO | |
} | |
} | |
public partial class User | |
{ | |
public supersede void Save() | |
{ | |
// TODO: | |
} | |
public supersede async Task SaveAsync() | |
{ | |
// TODO | |
} | |
} | |
// C# call-received default arguments | |
public class User | |
{ | |
public string First { get; set; } | |
public string Last { get; set; } | |
public void GetName | |
(string first = null, string last = null) | |
{ | |
if (first == null) | |
{ | |
first = First; | |
} | |
if (last = null) | |
{ | |
last = Last; | |
} | |
return $"{first} {last}"; | |
} | |
} | |
} | |
// C# call-received default arguments | |
public class User | |
{ | |
public string First { get; set; } | |
public string Last { get; set; } | |
public void GetName | |
(string first = this.First, string last = this.Last) | |
{ | |
return $"{first} {last}"; | |
} | |
} | |
// C# 6 Clone | |
public class User : ICloneable | |
{ | |
public int Id { get; set; } | |
public string First { get; set; } | |
public string Last { get; set; } | |
public object Clone() | |
{ | |
var user = new User | |
{ | |
First = this.First, | |
Last = this.Last, | |
}; | |
} | |
} | |
var user1 = new User | |
{ | |
Id = Guid.NewGuid(), | |
First = "Jerry", | |
Last = "Nixon", | |
} | |
var user2 = user1.Clone() as User; | |
user2.Id = Guid.NewGuid(); | |
// C# 7 compiler-provided With method | |
public class User // immutable | |
{ | |
public int Id { get; set; } | |
public string First { get; set; } | |
public string Last { get; set; } | |
} | |
var user1 = new User | |
{ | |
Id = Guid.NewGuid(), | |
First = "Jerry", | |
Last = "Nixon", | |
} | |
var user2 = user1.With(Id = Guid.NewGuid()); | |
var user2 = user with { Id = Guid.NewGuid() }; | |
// C# 6 output results | |
var url = "http://bing.com"; | |
Uri uri = null; | |
if (Uri.TryCreate(url, out Uri uri)) | |
{ | |
// TODO: use uri | |
} | |
else | |
{ | |
throw new Exception($"Failed to cast {url}"); | |
} | |
// C# 7 output var | |
var url = "http://bing.com"; | |
if (Uri.TryCreate(url, out var uri)) | |
{ | |
// TODO: use uri | |
} | |
else | |
{ | |
throw new Exception($"Failed to cast {url}"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment