Информация об изменениях

Сообщение Re: .NET 7 is Available Today от 10.11.2022 8:32

Изменено 10.11.2022 9:15 Serginio1

Re: .NET 7 is Available Today
Здравствуйте, Serginio1, Вы писали:

S>.NET 7 is Available Today


Понравилось https://devblogs.microsoft.com/dotnet/welcome-to-csharp-11/
Шаблоны списков

Pattern matching is one of the ongoing stories in C# that we just keep filling out. Pattern matching was introduced in C# 7 and since then it has grown to become one of the most important and powerful control structures in the language.

C# 11 adds list patterns to the story. With list patterns you can apply patterns recursively to the individual elements of list-like input – or to a range of them. Let’s jump right in with the generic algorithm from above, rewritten as a recursive method using list patterns:


T AddAll<T>(params T[] elements) where T : IMonoid<T> =>
elements switch
{
[] => T.Zero,
[var first, ..var rest] => first + AddAll<T>(rest),
};

There’s a lot going on, but at the center is a switch expression with two cases. One case returns zero for an empty list [], where Zero is defined by the interface. The other case extracts the first element into first with the var first pattern, and the remainder is extracted into rest using the .. to slice out all the remaining elements into the var rest pattern.

Read more about list patterns in the docs. https://learn.microsoft.com/ru-ru/dotnet/csharp/language-reference/operators/patterns#list-patterns


Ну вот и на стороне фунциональщиков праздник!
Re: .NET 7 is Available Today
Здравствуйте, Serginio1, Вы писали:

S>.NET 7 is Available Today


Понравилось https://devblogs.microsoft.com/dotnet/welcome-to-csharp-11/
Шаблоны списков

Pattern matching is one of the ongoing stories in C# that we just keep filling out. Pattern matching was introduced in C# 7 and since then it has grown to become one of the most important and powerful control structures in the language.

C# 11 adds list patterns to the story. With list patterns you can apply patterns recursively to the individual elements of list-like input – or to a range of them. Let’s jump right in with the generic algorithm from above, rewritten as a recursive method using list patterns:


T AddAll<T>(params T[] elements) where T : IMonoid<T> => 
    elements switch
{
    [] => T.Zero,
    [var first, ..var rest] => first + AddAll<T>(rest),
};


There’s a lot going on, but at the center is a switch expression with two cases. One case returns zero for an empty list [], where Zero is defined by the interface. The other case extracts the first element into first with the var first pattern, and the remainder is extracted into rest using the .. to slice out all the remaining elements into the var rest pattern.

Read more about list patterns in the docs. https://learn.microsoft.com/ru-ru/dotnet/csharp/language-reference/operators/patterns#list-patterns


Ну вот и на стороне фунциональщиков праздник!