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

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

Изменено 10.11.2022 8:47 Serginio1

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

S>.NET 7 is Available Today


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

Сопоставление шаблонов — это одна из постоянных историй в C #, которую мы просто продолжаем заполнять. Сопоставление с образцом было введено в C # 7, и с тех пор оно превратилось в одну из самых важных и мощных управляющих структур в языке.

C # 11 добавляет шаблоны списков в историю. С помощью шаблонов списков вы можете рекурсивно применять шаблоны к отдельным элементам ввода, подобного списку, или к их диапазону. Давайте сразу перейдем к описанному выше общему алгоритму, переписанному как рекурсивный метод с использованием шаблонов списков:


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

Там много чего происходит, но в центре находится выражение switch с двумя падежами. Один случай возвращает ноль для пустого списка[], где Zeroопределяется интерфейсом. В другом случае первый элемент извлекается firstс var firstпомощью шаблона, а оставшаяся часть извлекается с restпомощью.., чтобы вырезать все оставшиеся элементы в var restшаблоне.

Подробнее о шаблонах списков читайте в документации. 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


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