C# is here to stay, it is time to embrace one of the most popular languages of the future and learn what is coming. In this talk we will look at how C# has evolved over time, and why it is definitely here to stay!
11. using static System.Console;
using static System.Math;
class Program
{
static void Main(string[] args)
{
var angle = 90d;
WriteLine(Sin(angle));
}
}
12. using static System.Console;
using static System.Linq.Enumerable;
class Program
{
static void Main(string[] args)
{
foreach (var i in Range(0, 10))
{
WriteLine(i);
}
}
}
20. class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(filip.Address == null ? "No Address" : filip.Address.AddressLine1);
Console.WriteLine(filip?.Address?.AddressLine1 ?? "No Address");
var filip = new Person
{
Name = "Filip"
};
21. var people = new[]
{
new Person(),
null
};
WriteLine(people[0]?.Name);
WriteLine(people[1]?.Name);
24. $"My Width is {Width} and my Height is {Height}";
string.Format("My Width is {0} and my Height is {1}", Width, Height);
25. int age = 28;
var result = $"Hello there, I'm {age:D5} years old!";
WriteLine(result);
int num = 28;
object[] objArray1 = new object[] { num };
Console.WriteLine(
string.Format("Hello there, I'm {0:D5} years old!", objArray1));
26.
27. var people = new Dictionary<string, Person>
{
["Filip"] = new Person()
};
var answers = new Dictionary<int, string>
{
[42] = "the answer to life the universe and everything"
};
28.
29. class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
public override string ToString() =>
$"My Width is {Width} and my Height is {Height}";
}
36. 0b00001000
0xFF_00_FA_AF
var client = new WebClient
{
DownloadFileCompleted += DownloadFileCompletedHandler
};
var y = (var x = Foo(); Write(x); x * x);[field: NonSerialized]
public int Age { get; set; }
Binary literals and Digit separators Event initializers
Field targets on auto-properties Semicolon operator
37. class Person(string name, int age)
{
private string _name = name;
private int _age = age;
}
public void CalculateAgeBasedOn(int birthYear, out int age)
{
age = DateTime.Now.Year - birthYear;
}
CalculateAgeBasedOn(1987, out var age);
int Avg(params IEnumerable<int> numbers)
Primary Constructors Using params with IEnumerable
Declaration Expressions
38.
39.
40. public (int sum, int count) Calculate(IEnumerable<int> numbers)
{
var total = numbers.Sum();
return (total, numbers.Count());
}
https://github.com/dotnet/roslyn/issues/1207
var result = Calculate(Enumerable.Range(0, 100));
WriteLine($"Total is {result.sum}, with {result.count} numbers.");