C# 8 News

Well, in this video I bring you the new features that the upcoming C# 8 version will include.

 

1 - Nullable reference types.

One of the major issues in today’s applications is managing when something is null or could be null. C# 8 will allow us to prevent having null values in our code. To do this, we need to enable the expression #nulllable enable  below the using statements.

The reason it must be activated per file and not per project is because of all the legacy code already out there, it could cause huge issues if it was automatic.

 

 

2 - Asynchronous streams

Everyone is familiar with streams. We have a method that returns an IEnumerable<T>. Now, we can return an IAsyncEnumerable<T>. Of course, this new type has all the characteristics of Async, so we need to use all the necessary keywords, like await or Task, to work with it.

Read more about asynchronous programming.

 

 

3 - Array slice

The new version of C# allows us to specify a subrange within an array as shown in the example:

string[] nombres = { "nombre1",  "nombre2", "nombre3", "nombre4", "nombre5" };
foreach(string nombre in nombres[1..3]){
}

In this case, it will iterate from the first to the third element.

Another example is the ^ character, which indicates counting from the end. So if we specify nombres[3...^1], it will iterate from the third to the penultimate.

 

 

4 - Recursive patterns

In other words, patterns inside patterns, and here we also introduce the new switch statement, where we can pass more than one parameter as you can see below.

switch (p.firstname, p.middlename, p.lastname)
{
    case(string f, string m, string l):
        return $”{f}, {m[0]} {l}”;
    case(string f, null, string l):
        return $”{f}, {l}”;
.
. //resto de los casos
.
}

As you can see, the switch statement ends up being a huge mess, since there are a total of 9 cases, but with the new switch statement, things look much cleaner.

return (p.firstname, p.middlename, p.lastname) switch 
{
    (string f, string m, string l) => $”{f}, {m[0]} {l}”;
    (string f, null, string l) = > $”{f}, {l}”;
    ...//resto de los casos
}

Personally, I think they look much better as it is cleaner and much easier to read.

It’s worth noting that the compiler does not allow you to skip any of the options. We know that in this case there are a total of 8 "cases" in the switch. If we put only 7, the compiler will complain.

 

 

5 - Implicit type creation

Commonly, when we create an array of elements, we have to specify each element that the array will contain with new T.

From now on, it’s no longer necessary to specify the data type, since it’s referenced when you declare the array:

Personas[] arrayPersonas = {
    new (v1, v2, v3),
    new (v1, v2, v5)
}

 

 

6 - Default elements in interfaces

In my opinion, this is the most noteworthy new feature, since it’s a complete game changer in the way we think about interfaces today.

Let’s imagine we have the following structure:

public interface ITemario{
    void Ejercicio1();
    void Ejercicio2();
}
public class Temario1 : ITemario{
    public void Ejercicio1(){}
}
public class Temario2 : ITemario{
    public void Ejercicio1(){}
    public void Ejercicio2(){}
}

This structure, as we can see, will give an error because the Temario1 class does not contain all the methods included in the interface.

From now on in C# 8, we can do the following to prevent this from being a problem for the compiler:

public interface ITemario{
    void Ejercicio1();
    void Ejercicio2() => Console.WriteLine("Ejercicio no disponible");
}

By using =>, we have created a default value inside the interface.

This post was translated from Spanish. You can see the original one here.
If there is any problem you can add a comment bellow or contact me in the website's contact form

Uso del bloqueador de anuncios adblock

Hola!

Primero de todo bienvenido a la web de NetMentor donde podrás aprender programación en C# y .NET desde un nivel de principiante hasta más avanzado.


Yo entiendo que utilices un bloqueador de anuncios como AdBlock, Ublock o el propio navegador Brave. Pero te tengo que pedir por favor que desactives el bloqueador para esta web.


Intento personalmente no poner mucha publicidad, la justa para pagar el servidor y por supuesto que no sea intrusiva; Si pese a ello piensas que es intrusiva siempre me puedes escribir por privado o por Twitter a @NetMentorTW.


Si ya lo has desactivado, por favor recarga la página.


Un saludo y muchas gracias por tu colaboración

© copyright 2025 NetMentor | Todos los derechos reservados | RSS Feed

Buy me a coffee Invitame a un café