C# 8 – Features

Introduction: C# was developed by Microsoft. It is component-oriented language and was first released in 2002. This language has grown and changed across seven major versions. Anders Hejlsberg is the founder of this language. It also supports all object-oriented concepts like classes, objects, abstraction, encapsulation, inheritance, polymorphism etc. C# 8 comes with essentially bunch of new features listed below which were mainly introduced with the aim of improving the quality of code and making it more flexible :

  • Extension Everything: Extension methods were first introduced in 2007 in C# 3.0 which are essential for LINQ. A new syntax was proposed for the extension methods and was mainly introduced because the extension methods could be written but not the properties. Below is the example of the new syntax for the extension methods and properties.

          New Syntax:

          extension Exampleext extends  Example {

                 // methods and properties go here

           }

          The above example demonstrates how to create extension methods and properties for class “Example”.

  • Default Implementation on Interfaces: For declaring interfaces in C#, interface keyword is used. Whenever new methods or functions are added to the interfaces, the burden to implement these increases in various classes. So they should be implemented independently on each class. This feature is already available in other programming languages like Java. The main aim of this feature is that you can add new methods to an existing interface without breaking backward compatibility.

         Example:

         Let us take an example of the interface named as DemoInterface containing method show().

interface  DemoInterface {

               void  show ( );

          }

         Here is a class named “DemoClass” which implements an interface.

         class  DemoClass: DemoInterface {

               // Your code goes here…

          }

          Now with the new feature introduced in C# 8, we can add a new method to existing interface as shown below:

         interface  DemoInterface {

               void  show ( );

               void  count ( );

                      // some code…

                  }

            }

  • Nullable Reference Types: This feature was introduced to help prevent null exceptions. The earlier versions of C# do not allow you to decide whether the variable or parameter is supposed to be null or not. So, this new feature allows us to describe which parameter can be null i.e. reference types can no longer be nullable by default instead, they have marked nullable explicitly by using “Type?”. With this, if null is assigned to a non-nullable reference type, the compiler will generate a warning unless and until that particular variable with “?” is explicitly checked for null. Below  example is shown for this:

          class  Person {

                     public  string firstname;  //cannot be null

                     public  string? middlename;  //May be null

                     public  string lastname;  //cannot be null

              }

             The above code shows that the first name and last name of the person cannot be null while the middle name of the person can be null.

           There are several other new features proposed for the improvement of C# which include Improved extension support, Async stream, Async disposal etc.

Conclusion: This is just the glimpse of new features but the pace of innovation in C# is increasing rapidly. Since its evolution, it has now become one of the most powerful and popular languages. The new features introduced with the C# are mainly helpful to the developers. These upcoming features in C# language are public and are available to all for exploring. But the most important thing to remember here is that these features are still in progress and so, could be changed in the future with the warning.

Related Posts

Leave a comment