4. 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);
}
5. 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);
}
6. 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);
}
7. 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);
}
8. 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}",
9. 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}",
10. 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}",
11. 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);
}
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 => 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);
}
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 => 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);
}
}
}
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 $"Monkey {Name} lives in {Location} with Id of {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 => 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)}";
}
}
}
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 =>
$"Monkey {Name} lives in {Location} with Id of " +
"{(Id == 0 ? "Bananas" : 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 =>
$"Monkey {Name} lives in {Location} with Id of " +
"{(Id == 0 ? "Bananas" : Id)}";
}
19.
20. var length = 0;
if(customers != null)
length = customers.Length;
int? length = customers?.Length;
// null if customers is null
Customer first = null
if(length >0)
first = customers[0];
Customer first = customers?[0];
// null if customers is null
int length = customers?.Length ?? 0;
21. public void OnPropertyChanged(string name)
{
var changed = PropertyChanged;
if (changed == null)
return;
changed(this, new PropertyChangedEventArgs(name));
}
24. 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.
}
25. 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.
}
26. 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.
}
28. 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
*/
30. 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);
}
31. public static void GetMonkey(int count)
{
if(count < 0)
throw new ArgumentNullException("count");
var monkeys = GetMonkeys();
WriteLine("Name: " + monkeys[0].Name);
// prints "Name Sofia"
}
32. 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"
}
35. var numbers = new Dictionary<int, string> {
[7] = "seven",
[9] = "nine",
[13] = "thirteen"
};
36.
37.
38. public void Tally(IEnumerable<int> values, out int sum, out int count)
{ ... }
int s, c;
Tally(myValues, out s, out c);
Console.WriteLine($"Sum: {s}, count: {c}");
public Tuple<int, int> Tally(IEnumerable<int> values)
{ ... }
var t = Tally(myValues);
Console.WriteLine($"Sum: {t.Item1}, count: {t.Item2}");
39. public (int sum, int count) Tally(IEnumerable<int> values)
{ ... }
var t = Tally(myValues);
Console.WriteLine($"Sum: {t.sum}, count: {t.count}");
public async Task<(int sum, int count)> TallyAsync(IEnumerable<int> values)
{ ... }
var t = await TallyAsync(myValues);
Console.WriteLine($"Sum: {t.sum}, count: {t.count}");
40. var t = new (int sum, int count) { sum = 0, count = 0 }; // out of the box today syntax
public (int sum, int count) Tally(IEnumerable<int> values)
{
var s = 0;
var c = 0;
foreach (var value in values)
{
s += value;
c++;
}
return (s, c); // target typed to (int sum, int count)
}
public (int sum, int count) Tally(IEnumerable<int> values)
{
var res = (sum: 0, count: 0); // infer tuple type from
//names and values
foreach (var value in values)
{
res.sum += value;
res.count++;
}
return res;
}
41. var v = expr as Monkey;
if (v != null)
{
// code using v
}
if (expr is Monkey v)
{
// code using v
//v.Name
}
int? x = 3;
if (x is int v)
{
// code using v
}
42. Type? v = x?.y?.z;
if (v.HasValue)
{
var value = v.GetValueOrDefault();
// code using value
}
if (x?.y?.z is Type value)
{
// code using value
}
43. public class Dog
{
public string Name { get; private set; }
public Dog(string name)
{
Name = name;
}
public void Bark() { }
}
Dog! myMandatoryDog = new Dog("Mandatory");
Dog? myNullableDog = new Dog("Nullable");
Dog myGeneralDog = new Dog("General");
Dog! mandatoryDog = null; // Compiler Error.
Dog? nullableDog = null; // OK.