hosting tips

C# 11.0 : List patterns

List Patterns, a game-changing feature, was introduced in C# 11. These patterns make it simple to match an array or a list to a sequence of elements. Let’s look at three different list pattern matching strategies.

1. Discard Pattern

When you know the length of the sequence, the Discard Pattern is handy. For example, comparing one array of five integers against another array with the same values.

var ids = new[] { 1, 2, 3, 4, 5 };
if (ids  is [1, 2, 3, 4, 5])
{
    Console. WriteLine("Its matched");
}

To match regardless of the values, you can use the underscore (_).

if (ids  is [_, _, _, _, _])
{
    Console. WriteLine("Its matched");
}

2. Range Pattern

When you’re unsure about the sequence length, the Range Pattern comes into play. Use two dots (..) to specify any number of elements. For instance.

if (ids  is [1, ..])
{
    Console. WriteLine("Its matched");
}

Combining range with the discard pattern.

if (ids  is [1, _, 3, ..])
{
    Console. WriteLine("Its matched");
}

3. Var Pattern

The Var Pattern allows you to declare variables and assign matched elements. These variables are accessible in the same scope.

if (ids  is [var one, var two, ..])
{
    Console.WriteLine($"{one}, {two}");
}

These C# List patterns empower you to write more concise and expressive code, enhancing your ability to work with sequences effectively. Dive into the versatility of list patterns and take your C# coding to the next level!

ASP.NET Core 8 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.