1. PRESENTS
Coimbatore, Tamil Nadu, India Dec 22, 2018
Brought to you by
Microsoft Connect(); 2018 - Local Event
Muralidharan Deenathayalan
Technical Architect at Quanticate
What's New in C# 8.0
2. Brought to you by
• C# - A quick definition
• Evolution history of C#
• New features of C# 8.0
• Demo
• How I can learn more?
• Q & A
• Reference links
2
Agenda
3. Brought to you by
• Nullable reference type
• Implicitly-typed new-expressions
• Ranges and indices
• Default implementations of interface members
• Recursive patterns
• Switch expressions
• Asynchronous streams
3
New features of C# 8.0
5. Brought to you by
5
Evolution history of C#
C# Version .net Fx Version Visual Studio Version Date
1.0 NET Framework 1.0 Visual Studio .NET 2002 January 2002
1.1, 1.2 .NET Framework 1.1 Visual Studio .NET 2003 April 2003
C# 2.0 .NET Framework 2.0 Visual Studio 2005 November 2005
C# 3.0 .NET Framework 3.0 Visual Studio 2008 November 2006
C# 3.5 .NET Framework 3.5 Visual Studio 2010 November 2007
C# 4.0 .NET Framework 4 Visual Studio 2010 April 2010
C# 5.0 .NET Framework 4.5 Visual Studio 2012
Visual Studio 2013
August 2012
C# 6.0 .NET Framework 4.6 Visual Studio 2015 July 2015
C# 7.0 .NET Framework 4.6.2 Visual Studio 2017 March 2017
C# 7.1 .NET Framework 4.7 Visual Studio 2017 v 15.3 August 2017
C# 7.2 .NET Framework 4.7.1 Visual Studio 2017 v 15.5 November 2017
C# 7.3 .NET Framework 4.7.2 Visual Studio 2017 v 15.7 May 2018
C# 8.0 - beta .NET Framework 4.8 Visual Studio 2019
6. Brought to you by
• C# variable types: Primitives and Reference types
• Primitive data type
• int, char can’t accept null
• Will have default value of 0 or depends on its data type
• To support, make is nullable like int?
• Ex : int?, char?
• Reference type
• Class etc
• Accepts null
• Default value will be null unless initialized
• Ex: string 6
Nullable reference type
7. Brought to you by
• Allow developers to express whether a variable, parameter or
result of a reference type is intended to be null or not.
• Provide optional warnings when such variables, parameters and
results are not used according to their intent.
7
Nullable reference type
8. Brought to you by
• Select C# version to 8.0 (beta)
• Add #nullable enable in the class level
8
Nullable reference type
string myname = null; //will throw warning
Console.WriteLine(myname.Length); //Will throw below warnings
• WarningCS8600 Converting null literal or possible null value to non-nullable type.
• WarningCS8602 Possible dereference of a null reference.
9. Brought to you by
• Select C# version to 8.0 (beta)
• Add #nullable enable in the class level
Converting to nullable as below.
9
Nullable reference type
string? myname = null; //will throw warning
Console.WriteLine(myname.Length); //Will throw below warnings
• WarningCS8602 Possible dereference of a null reference.
10. Brought to you by
To avoid this warning, add null check
10
Nullable reference type
string? myname = null;
if (myName != null)
Console.WriteLine(myName.Length);
else
Console.WriteLine(0);
11. Brought to you by
11
Implicitly-typed new-expressions
Author[] authors =
{
new Author("William", "Shakespeare"),
new Author("John", " Ford")
};
12. Brought to you by
12
Implicitly-typed new-expressions
Author[] authors =
{
new ("William", "Shakespeare"),
new ("John", " Ford")
};//new Syntax. No need specify the type here
13. Brought to you by
• Simple syntax for slicing out a part of an array
• New type is called ‘Range’
• Endpoint is exclusive
• new ^ operator, meaning "from end"
• Examples,
• 1..4
• ..1
• 1..
• 1..^2
• ^2..
13
Ranges and indices
14. Brought to you by
14
Ranges and indices
foreach (var name in names[1..4])
{
Console.WriteLine(name);
} // Getting 1,2 and 3rd element
Range range = 1..4;
foreach (var name in names[range])
{
Console.WriteLine(name);
}//Range variable is used
foreach (var name in names[1..^ 1])
{
Console.WriteLine(name);
}//Starting from 1st element and ending from last 1 element
15. Brought to you by
• Default interface methods (also known as virtual extension
methods)
• C# Addresses the diamond inheritance problem that can occur
with default interface methods by taking the most specific
override at runtime.
15
Default implementations of interface
members
16. Brought to you by
16
Default implementations of interface
members
interface IDefaultInterfaceMethod
{
public void DefaultMethod()
{
Console.WriteLine("I am a default method in the interface!");
}
}
class AnyClass : IDefaultInterfaceMethod
{
}
17. Brought to you by
17
Default implementations of interface
members
IDefaultInterfaceMethod anyClass = new AnyClass();
anyClass.DefaultMethod();
18. Brought to you by
18
Default implementations of interface
members
AnyClass anyClass = new AnyClass();
anyClass.DefaultMethod(); //compilation error
• AnyClass does not contain a member DefaultMethod.
• That is the proof that the inherited class does not know anything
about the default method.
19. Brought to you by
19
Recursive patterns
Author author = new Author("William", "Shakespeare");
switch (author.FirstName, author.LastName)
{
case (string fn, string ln):
return $"{fn} {ln} ";
case (string fn, null):
return $"{fn} ";
case (null, ln):
return $"Mr/Mrs {ln} ";
case (null, null):
return $"New author ";
}
21. Brought to you by
• Asynchrous programming techniques provide a way to improve a
program's responsiveness
• Async/Await pattern debuted in C# 5, but is limited to returning a
single scalar value.
• C# 8 adds Async Streams, which allows an async method to return
multiple values
• async Task < int > DoAnythingAsync(). The result of
DoAnythingAsync is an integer (One value).
• Because of this limitation, you cannot use this feature with yield
keyword, and you cannot use it with the async IEnumerable < int >
(which returns an async enumeration).
21
Asynchronous streams
22. Brought to you by
• async/awaiting feature with a yielding operator = Async Stream
• asynchronous data pull or pull based enumeration or async
sequence in F#
22
Asynchronous streams
24. Brought to you by
• https://github.com/dotnet/csharplang/tree/master/proposals
• https://github.com/dotnet/roslyn/blob/master/docs/Language%
20Feature%20Status.md
• https://github.com/dotnet/roslyn
24
How I can learn more?
26. Brought to you by
• https://github.com/dotnet/coreclr/issues/21379 for
IAsyncEnumberable<T>
• https://www.infoq.com/articles/default-interface-methods-cs8
• https://www.infoq.com/articles/cs8-ranges-and-recursive-
patterns
26
References
27. Brought to you by
27
Keep in touch
Muralidharan Deenathayalan
Blog : www.codingfreaks.net
Github : https://github.com/muralidharand
LinkedIn : https://www.linkedin.com/in/muralidharand
Twitter : https://twitter.com/muralidharand