c# is the future
TRANSCRIPT
I’m Filip Ekberg
Readify
@fekberg
Author. Blogger. Speaker. .NET MVP. Xamarin MVP. Geek.
Senior Software Engineer @
C# 2.0Generics
Partial types
Anonymous methods
Iterators
Nullable types
Getter/Setter separate accessibility
Static classes
And more..
@fekberg
Implicitly typed local variables
Object and collection initializers
Auto-properties
Anonymous types
Extension methods
Query expressions
Lambda expressions
Expression trees
And more..
Dynamic binding
Named and optional parameters
Generic co- and contravariance
And more..
C# 3.0
C# 4.0
Asynchronous methods
Caller info attributes
C# 5.0
Starting fresh with the new compiler• .NET Compiler Platform (codename Roslyn)• Microsoft decides to re-write the compiler• Compiler written in C#• Easier to extend and maintain• Trusting the compiler to solve complexity for us• Let us be less verbose (syntactic sugar)• Open Source!
@fekberg
Using Statements for Static Members
@fekberg
class Program{ static void Main(string[] args) { var angle = 90d; Console.WriteLine(Math.Sin(angle)); }}
Using Statements for Static Members
@fekberg
using static System.Console;using static System.Math;
class Program{ static void Main(string[] args) { var angle = 90d; WriteLine(Sin(angle)); }}
Using Statements for Static Members
@fekberg
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); } }}
Auto-Property Initializers
@fekberg
class Person{ public string Name { get; set; }}
class Person{ public string Name { get; } = "Anonymous";}
Auto-Property Initializers
@fekberg
class Person{ public string Name { get; }
public Person() { Name = "Filip"; }}
Auto-Property Initializers
@fekberg
class Person{ private readonly string <Name>k__BackingField = "Filip";
public string Name { get { return this.<Name>k__BackingField; } }}
Await inside Catch & Finally block
@fekberg
public async Task DownloadAsync(){
}
try { } catch { await Task.Delay(2000); } finally { await Task.Delay(2000); }
Null-conditional operators
@fekberg
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"};
Null-conditional operators
@fekberg
var people = new[]{ new Person(), null};
WriteLine(people[0]?.Name);
WriteLine(people[1]?.Name);
Null-conditional operators
@fekberg
Person[] people = null;
WriteLine(people?[0]?.Name);
Person[] people = null;Console.WriteLine(
(people != null) ? ((people[0] == null) ? null :
people[0].Name) : null
);
String interpolation
@fekberg
$"My Width is {Width} and my Height is {Height}";
string.Format("My Width is {0} and my Height is {1}", Width, Height);
String interpolation
@fekberg
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));
Dictionary Intializers
@fekberg
var people = new Dictionary<string, Person>{ ["Filip"] = new Person()};
var answers = new Dictionary<int, string>{ [42] = "the answer to life the universe and everything"};
Expression-bodied members
@fekberg
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}";
}
nameof operator
@fekberg
static void Main(string[] args){ WriteLine($"Parameter name is: {nameof(args)}");}
Exception Filters
@fekberg
try{ throw new CustomException { Severity = 100 };}catch (CustomException ex) when (ex.Severity > 50){ Console.WriteLine("*BING BING* WARNING *BING BING*");}catch (CustomException ex){ Console.WriteLine("Whooops!");}
C# 6.0Using Statements for Static Members
Auto-Property Initializers
Await inside Catch & Finally blocks
Null-conditional operators
String interpolation
Dictionary Initializers
Expression-bodied members
Exception Filters
nameof operator
And more..
@fekberg
C# 2.0Generics
Partial types
Anonymous methods
Iterators
Nullable types
Getter/Setter separate accessibility
Static classes
And more..
Implicitly typed local variables
Object and collection initializers
Auto-properties
Anonymous types
Extension methods
Query expressions
Lambda expressions
Expression trees
And more..
Dynamic binding
Named and optional parameters
Generic co- and contravariance
And more..
C# 3.0
C# 4.0Asynchronous methods
Caller info attributes
C# 5.0
Withdrawn features 1 / 2
@fekberg
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
Withdrawn features 2 / 2
@fekberg
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
Why care about withdrawn features?
@fekberg
• Indicates the evolution and on-going work• Shows there are no promises until it is RTM• Withdrawn does not mean “Will never come to C#”
Tuples (Multiple return types)
@fekberg
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.");
• Requires a record type• Record types uses primary constructors (see withdrawn C# 6.0 features)
Pattern Matching and Record Types
@fekberg
https://github.com/dotnet/roslyn/issues/206
public record class Point(int X, int Y);
if (expr is Point point) { // Use point here}
More potential C# 7.0 features
@fekberg
• Nullability tracking• Asynchronous streams• Declaration expressions (see withdrawn C# 6.0 features)
• Binary literals and Digit separators (see withdrawn C# 6.0
features)
• And more…
Join the discussion
C# 7 Work List of Featureshttps://github.com/dotnet/roslyn/issues/2136
@fekberg
Endless potential
• Language feature discussions on Github• Compiler API at your disposal• Write code analysers and refactoring
packages• C# evolves and grows each release
@fekberg