After 18 years of existence, C# is still as young and "sharp" as ever. Indeed, by open-sourcing its compiler and design, Microsoft strengthened the position of C# in the market by making it available to all developers on GitHub.
Contributing and voting to set the language roadmap are the main contributors to its success in the last few years. Last month, the C# language team in Microsoft announced the release of the preview version of VS 2019 and .NET Core 3.0. The exciting thing about it is that this version includes the new C# 8 that we can play with to test some of the new features.
In this article, I'll walk you through some of the new features introduced in the 8th version of C# and show some use cases.
Ranges and Indices
C# 8 introduces two new types and operators for collections manipulation and indexing. Basically, we will have a more interesting and elegant way to index and slide collections.
New types - System.Index and System.Range.
New Operators - .. and ^.
Let's see some examples.
- Index d1 = 2; // number 2 from beginning
- Index d2 = ^ 3; // number 3 from end
- string[] week = {
- "Monday",
- "Tuesday",
- "Wednesday",
- "Thursday",
- "Friday",
- "Saturday",
- "Sunday"
- };
- Console.WriteLine($ "{week[d1]}, {week[d2]}"); // "Wednesday , Friday “
The element selection is 0-based if you are counting from the beginning and 1-based if you are counting from the end.
- Get the last element.
- //Old Style
- var lastDay = week[week.Count -1]; // Sanday
- //New Style
- var lastDay = week[^1]; // Sanday
- Get a range of elements.
- //Old Style
- var days = week.ToList().GetRange(2, 3); // Wednesday,Thursday,Friday
- //or
- var days = week.Skip(2).Take(3); // Wednesday,Thursday,Friday
- //New Style
- var days = week[1.. ^ 2]; // Wednesday,Thursday,Friday
- // [1..^2] mean
- // 1 : skip elements from the begining until the index 1
- // ^2 : skip 2 element from the end
Limitation
This new feature will be available in any framework implementing .NET Standard 2.1. For example, it will be available in .NET Core 3.0 but not in the .NET framework 4.8. If you want more information about .NET Standard you can check this article.
Default Implementations of Interface Members
For this part, we will imagine that we have a three-year-old application with a bunch of classes and interfaces. Thirty of these classes implement an Interface called IPrintable. This interface includes a signature for each printer operation.
Let's say you want to add a new method that does exactly the same thing as "Print" but has a different signature. In this case, there is no way around reimplementing the new method for all 30 classes.
With C# 8, you can define a default implementation for the new interface member.
Let's check how this works.

jeevi anuPosted Nov 16, 2019, 12:20 AM
VERY USEFUL TO EVERYONE AND THANKS FOR SHARING THIS.
jeevi anuPosted Oct 24, 2019, 12:45 AM
Very informative. Thanks for the post I have book marked this blog
Madan ShekarPosted Feb 23, 2019, 2:40 AM
Nice information , thanks for sharing
Madan ShekarPosted Feb 23, 2019, 2:40 AM
Nice information
Subin ThomasPosted Feb 22, 2019, 5:37 AM
Nice thanks for sharing
Junaid ShahidPosted Feb 22, 2019, 1:35 AM
It's nice and you have typo "Sanday" search near "Get the last element". This feature is quite nice and expedite development in looping scenarios.
Dinesh BeniwalPosted Feb 20, 2019, 11:08 PM
Thank you for sharing.
Shamim UddinPosted Feb 20, 2019, 11:16 AM
Thank you sir for your informative article.
Mahesh ChandPosted Feb 20, 2019, 9:31 AM
Thank you, Ahd Ben.