C# is being continually updated with new features. C# 6.0 introduced many new features such as auto-property initializers, getter-only auto-properties, and null propagation. C# 7.0 may include additional features like binary literals, nested methods, and pattern matching. The .NET Compiler Platform makes it easier for Microsoft to improve and extend the C# language over time.
7. Page
The state of the Compiler
• It’s been a black box
• Roslyn to the rescue!
• .NET Compiler Platform
• Microsoft decides to re-write the compiler
• Compiler written in C#
• Easier to extend and maintain
• Open Source!
7
10. Page
Auto-property initializers in a nutshell
10
class Person
{
public string Name { get; set; }
}
class Person
{
public string Name { get; } = "Anonymous";
}
= "Anonymous";
15. Page
Parameter-less struct constructors in a nutshell
15
Read Only!
struct Point
{
private readonly int <X>k__BackingField;
private readonly int <Y>k__BackingField;
public int X
{
get
{
return this.<X>k__BackingField;
}
}
public int Y
{
get
{
return this.<Y>k__BackingField;
}
}
public Point()
{
this.<X>k__BackingField = 100;
this.<Y>k__BackingField = 100;
}
}
16. Page
Parameter-less struct constructors in a nutshell
16
Read Only!
struct Point
{
public int X { get; }
public int Y { get; }
public Point(int x, int y)
{
X = x;
Y = y;
}
public Point() : this(100, 100)
{
}
}
18. Page
Using Statements for Static Members in a nutshell
18
class Program
{
static void Main(string[] args)
{
var angle = 90d;
Console.WriteLine(Math.Sin(angle));
}
}
19. Page
Using Statements for Static Members in a nutshell
19
using System.Console;
using System.Math;
class Program
{
static void Main(string[] args)
{
var angle = 90d;
WriteLine(Sin(angle));
}
}
20. Page
Using Statements for Static Members in a nutshell
20
using System.Console;
using System.Linq.Enumerable;
class Program
{
static void Main(string[] args)
{
foreach (var i in Range(0, 10))
{
WriteLine(i);
}
}
}
22. Page
Dictionary Initializers in a nutshell
22
var people = new Dictionary<string, Person>
{
[“abhishek"] = new Person()
};
var answers = new Dictionary<int, string>
{
[42] = "the answer to life the universe and everything"
};
28. Page
Null propagation in a nutshell
28
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(abhishek.Address.AddressLine1);
var abhishe = new Person
{
Name = “Abhishek"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
29. Page
Null propagation in a nutshell
29
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(abhishek.Address.AddressLine1);
var abhishek = new Person
{
Name = “abhishek"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
30. Page
Null propagation in a nutshell
30
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(abhishek.Address.AddressLine1);Console.WriteLine(abhishek.Address == null ? "No Address" : abhishek.Address.AddressLine1);
var abhishek = new Person
{
Name = “abhishek"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
31. Page
Null propagation in a nutshell
31
class Person
{
public string Name { get; set; }
public Address Address { get; set; }
}
Console.WriteLine(ahishek.Address.AddressLine1);Console.WriteLine(abhishek.Address == null ? "No Address" : abhishek.Address.AddressLine1);
Console.WriteLine(abhishek?.Address?.AddressLine1 ?? "No Address");
var abhishek = new Person
{
Name = “abhishek"
};
class Address
{
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
}
32. Page
Null propagation in a nutshell
32
@fekberg
var people = new[]
{
new Person(),
null
};
WriteLine(people[0]?.Name);
WriteLine(people[1]?.Name);
33. Page
Null propagation in a nutshell
33
Person[] people = null;
WriteLine(people?[0]?.Name);
Person[] people = null;
Console.WriteLine(
(people != null) ?
((people[0] == null) ? null : people[0].Name)
: null
);
35. Page
Expression-bodied members in a nutshell
35
class Rectangle
{
public double Width { get; set; }
public double Height { get; set; }
public double Area => Width * Height;
}
36. Page
Expression-bodied members in a nutshell
36
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}";
}
38. Page
String interpolation in a nutshell
38
public override string ToString() =>
"My Width is {Width} and my Height is {Height}";
public override string ToString() =>
$"My Width is {Width} and my Height is {Height}";
Syntax will change in a later release to the following:
39. Page
String interpolation in a nutshell
39
public override string ToString() =>
"My Width is {Width} and my Height is {Height}";
public override string ToString()
{
object[] args = new object[] { this.Width, this.Height };
return string.Format("My Width is {0} and my Height is {1}", args);
}
40. Page
String interpolation in a nutshell
40
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));
42. Page
nameof operator in a nutshell
42
static void Main(string[] args)
{
WriteLine("Parameter name is: {nameof(args)}");
}
43. Page
nameof operator in a nutshell
43
public double CalculateArea(int width, int height)
{
if (width <= 0)
{
throw new ArgumentException("Parameter {nameof(width)} cannot be less than 0");
}
return width * height;
}
45. Page
How to try?
45
• InstallVisual Studio Preview 2 and create project with experimental
compilation symbol.
46. Page
What about C# 7.0?
• Binary literals and Digit separators
46
0b00001000
0xFF_00_FA_AF
47. Page
What about C# 7.0?
• Event initializers
47
var client = new WebClient
{
DownloadFileCompleted +=
DownloadFileCompletedHandler
};
48. Page
What about C# 7.0?
• Nested Methods
48
Void MyMethod(){
void calculator(int x)
{
return x * 2;
};
return calculator(10);
}
49. Page
What about C# 7.0?
• Field targets on auto-properties
49
[field: NonSerialized]
public int Age { get; set; }
50. Page
What about C# 7.0?
• Inherent use ofTuples and language integration
50
(int x, int y) MyFunction(){
Console.WriteLine(“My Tuple is called”);
}
51. Page
What about C# 7.0?
Inherent use ofTuples and language integration
51
public class Person
{
public readonly (string firstName, string lastName) Names; // a tuple
public Person((string FirstName, string LastName)) names, int Age)
{
Names = names;
}
}
52. Page
What about C# 7.0?
Inherent use ofTuples and language integration
52
(string first, string last) = GetNames("Inigo Montoya")
53. Page
What about C# 7.0?
• Using params with IEnumerable
53
int Avg(params IEnumerable<int> numbers)
54. Page
What about C# 7.0?
• Declaration Expressions
54
public void CalculateAgeBasedOn(int birthYear, out int age)
{
age = DateTime.Now.Year - birthYear;
}
CalculateAgeBasedOn(1987, out var age);
55. Page
What about C# 7.0?
• Primary Constructors
55
class Person(string name, int age)
{
private string _name = name;
private int _age = age;
}
56. Page
What about C# 7.0?
• Pattern Matching
56
object obj;
// ...
switch(obj) {
case 42:
// ...
case Color.Red:
// ...
case string s:
// ...
case Point(int x, 42) where (Y > 42):
// ...
case Point(490, 42): // fine
// ...
default:
// ...
}
57. Page
What about C# 7.0?
• Async Streams
• IAsyncEnumerable will also work withAwait. It has MoveNextAsync and Current
property like IEnumerable, and you can call them using foreach loops.
57
58. Page
What about C# 7.0?
• Immutable Objects
• Inherently thread-safe
• Makes it easier to use and reason about code
• Easier to parallelize your code
• Reference to immutable objects can be cached, as they won’t change
58
public immutable class Point
{
public Point(int x, int y)
{
x = x;
Y = y;
}
public int X { get; }
public int Y { get; }
}
59. Page
Summary
• C# 6.0 is awesome
• There’s a lot of change in C# 6.0
• .NET Compiler Platform ("Roslyn") makes it easy for Microsoft
to improve the language
• There’s a lot of interesting features that could go in C# 7.0
60