Article Overview
- Background
- Pre-requisites
- Indices and Ranges
- Default Interface Members
- Static Local Functions
- 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:
- Indicing can be specified as an offset from the end as following:
- Retrieve the last word with the ^1 index:
Now, let us understand it by example.
Example-1
- Index d1 = 2;
- Index d2 = ^ 3;
- string[] week = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
- Console.WriteLine($"{week[d1]}, {week[d2]}");
Output
Wednesday, Friday
Example-2
-
- var lastDayOldStyle = week[week.Length -1];
-
- 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.
Now, let us understand by example.
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.
Now, let us understand by example.
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,