C# - Top 10 New Features (Part 2 Of 4)

In this article, I will cover and discuss the next three key features and enhancements to C# 8.0, such as "Indices and Ranges", "Default Interface Members", and "Static Local Functions" along with examples of each one.

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.
For your reference, I have kept all the examples in a single .cs file and uploaded with this article for your easy to use.
 

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
  1. Index d1 = 2; // number 2 from beginning    
  2. Index d2 = ^ 3; // number 3 from end   

  3. string[] week = {   "Monday",   "Tuesday",      "Wednesday",    "Thursday",     "Friday",   "Saturday",     "Sunday"  };    

  4. Console.WriteLine($"{week[d1]}, {week[d2]}");  
Output
 
Wednesday, Friday
 
Example-2
  1. //Old Style    
  2. var lastDayOldStyle = week[week.Length -1];  

  3. //New Style    
  4. var lastDayNewStyle = week[^1];  

  5. Console.WriteLine($"{lastDayOldStyle}, {lastDayNewStyle}");  
Output
 
Sunday, Sunday
 
Example-3
  1. var days2 = week[1.. ^ 2];    
  2.   
  3. foreach(var item in days2)  
  4. {  
  5.     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.
Now, let us understand by example.

Example-1
  1. Printer p = new Printer();  
  2. p.Print(".Net""Fiddle"); 

  3. IFax obj = new Fax();  
  4. obj.Print(); 

  5. public interface IPrintable {    
  6.     void Print(string header, string text);    
  7. }   

  8. public class Printer: IPrintable {    
  9.     public void Print(string header, string text) {    
  10.         Console.WriteLine($"header = {header} and text = {text}");  
  11.     }    
  12. }   

  13. public interface IFax    
  14. {    
  15.      public void Print()    
  16.      {    
  17.         Console.WriteLine("Hello World!");    
  18.      }    
  19. }  

  20. public class Fax : IFax    
  21. {    
  22. }    

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.
Now, let us understand by example.
 
Example-1
  1. Employee emp1 = new Employee();  
  2.   
  3. emp1.MethodStaticLocalFunction(515);  
  4.   
  5. public class Employee  
  6. {  
  7.     public void MethodWithStaticLocalFunction(int input)  
  8.     {  
  9.         Console.WriteLine($"Inside MethodWithStaticLocalFunction, input: {input}.");  
  10.         StaticLocalFunction(input);  
  11.         static void StaticLocalFunction(int input)  
  12.         {  
  13.             Console.WriteLine($"Inside StaticLocalFunction, input: {input}.");  
  14.         }  
  15.     }  

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,