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
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
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
Brought to you by
4
C# - A quick definition
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
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
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
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.
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.
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);
Brought to you by
11
Implicitly-typed new-expressions
Author[] authors =
{
new Author("William", "Shakespeare"),
new Author("John", " Ford")
};
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
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
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
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
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
{
}
Brought to you by
17
Default implementations of interface
members
IDefaultInterfaceMethod anyClass = new AnyClass();
anyClass.DefaultMethod();
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.
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 ";
}
Brought to you by
20
Switch expressions
Author author = new Author("William","Shakespeare");
return (author.FirstName, author.LastName) switch
{
(string fn, string ln) => $"{fn} {ln}",
(string fn, null) => $"{ fn}" ,
(null, ln) => $"Mr/Mrs { ln}" ,
(null, null) => $"New author "
};
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
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
Brought to you by
23
Demo
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?
Brought to you by
25
Q & A
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
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

What's new in C# 8.0 (beta)

  • 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 youby • 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 youby • 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
  • 4.
    Brought to youby 4 C# - A quick definition
  • 5.
    Brought to youby 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 youby • 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 youby • 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 youby • 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 youby • 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 youby 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 youby 11 Implicitly-typed new-expressions Author[] authors = { new Author("William", "Shakespeare"), new Author("John", " Ford") };
  • 12.
    Brought to youby 12 Implicitly-typed new-expressions Author[] authors = { new ("William", "Shakespeare"), new ("John", " Ford") };//new Syntax. No need specify the type here
  • 13.
    Brought to youby • 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 youby 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 youby • 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 youby 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 youby 17 Default implementations of interface members IDefaultInterfaceMethod anyClass = new AnyClass(); anyClass.DefaultMethod();
  • 18.
    Brought to youby 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 youby 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 "; }
  • 20.
    Brought to youby 20 Switch expressions Author author = new Author("William","Shakespeare"); return (author.FirstName, author.LastName) switch { (string fn, string ln) => $"{fn} {ln}", (string fn, null) => $"{ fn}" , (null, ln) => $"Mr/Mrs { ln}" , (null, null) => $"New author " };
  • 21.
    Brought to youby • 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 youby • async/awaiting feature with a yielding operator = Async Stream • asynchronous data pull or pull based enumeration or async sequence in F# 22 Asynchronous streams
  • 23.
    Brought to youby 23 Demo
  • 24.
    Brought to youby • 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?
  • 25.
    Brought to youby 25 Q & A
  • 26.
    Brought to youby • 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 youby 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