Article Overview
- Background
- Pre-requisites
- Indices and Ranges
- Points
- Example
- Default Interface Members
- Points
- Example
- Static Local Functions
- Points
- Example
- Summary
Background
I have divided this whole article into 4 parts to make it easy to understand. This article mainly focuses on three key features and enhancements to C# 8.0,
- Indices and Ranges
- Default Interface Members
- Static Local Functions
Here, I have kept all the implementation details with a complete example.
Prerequisites
- You should have basic knowledge of C#.
- In Part 1 of 4; I had explained “How to compile/test with C# 8.0 in MS Visual Studio OR online”, “Key features and enhancements to the C# 8.0”, and the “Switch Expression” feature.
- So, it will be good if you read the first part, i.e., “C# - Top 10 New Features (Part 1 Of 4)” of this article.
Indices and Ranges
C# 8.0 introduced a new syntax for expressing a range value.
- Starting index of a range is inclusive, and the ending is exclusive:
- Range range = 1..5;
- Indicing can be specified as an offset from the end as following:
- Range range = 1..^1;
- Retrieve the last word with the ^1 index:
- words[^1]
Now, let us understand it by example.
Example-1
- 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]}");
Output
Wednesday, Friday
Example-2
- //Old Style
- var lastDayOldStyle = week[week.Length -1];
- //New Style
- var lastDayNewStyle = week[^1];
- Console.WriteLine($"{lastDayOldStyle}, {lastDayNewStyle}");
Output
Sunday, Sunday
Example-3
- var days2 = week[1.. ^ 2];
- foreach(var item in days2)
- {
- Console.WriteLine($"{item}");
- }
Output
Tuesday, Wednesday, Thursday, Friday
Default Interface Members
Below are the key syntax improvements,
- You can add members along with implementation to the interfaces.
- It enables API authors to add methods to the interface in later versions without breaking source compatibility with existing implementations of that interface.
- Using it, existing implementations inherit default implementation.
- It allows us to add new functionality to the interface of the libraries and it also ensures the backward compatibility with code written for older versions of those interfaces.
Example-1
- Printer p = new Printer();
- p.Print(".Net", "Fiddle");
- IFax obj = new Fax();
- obj.Print();
- public interface IPrintable {
- void Print(string header, string text);
- }
- public class Printer: IPrintable {
- public void Print(string header, string text) {
- Console.WriteLine($"header = {header} and text = {text}");
- }
- }
- public interface IFax
- {
- public void Print()
- {
- Console.WriteLine("Hello World!");
- }
- }
- public class Fax : IFax
- {
- }
Static Local Functions
Below are the key syntax improvements,
- You can add a static modifier to the local function.
- The static local function cannot contain the reference to the variable.
- It will prevent using a variable from containing method in a local function and at the same time avoid performance costs related to making them available.
Example-1
- Employee emp1 = new Employee();
- emp1.MethodStaticLocalFunction(515);
- public class Employee
- {
- public void MethodWithStaticLocalFunction(int input)
- {
- Console.WriteLine($"Inside MethodWithStaticLocalFunction, input: {input}.");
- StaticLocalFunction(input);
- static void StaticLocalFunction(int input)
- {
- Console.WriteLine($"Inside StaticLocalFunction, input: {input}.");
- }
- }
- }
Summary
Now, I believe you will be able to implement "Indices and Ranges", "Default Interface Members", and "Static Local Functions" in C# 8.0.
In my next article, I will cover next three features such as "Using Declarations", "Nullable Reference Types", and "Readonly Struct Members".
Previous parts of this series,

RyderPosted Feb 7, 2020, 5:48 AM
Easy to understand...
Dhruvik SathvaraPosted Oct 10, 2019, 12:40 AM
Good article...
Faisal PathanPosted Oct 8, 2019, 11:40 PM
Hi NIce article, but the output of example 3 is wrong, I think the output should be "Tuesday Wednesday Thursday Friday", please update.
Sourav Kumar DasPosted Oct 4, 2019, 11:27 PM
Good Article for begineers.