Monday, 18 November 2024

ASP.NET Hosting Tutorial: Understanding Param Collection in C# 13

Leave a Comment

The params keyword in C# lets developers pass any number of arguments to a method, making it easier to work with lists or arrays. It was first introduced in C# 1.0 and is still useful today. This blog will dive into the new feature in C# 13 related to params usage


Prerequisites

  • Visual Studio 2022 – 17.12 version
  • .NET 9.0

You can download the Visual Studio Community edition from here.

What is param collection?

The params keyword in C# lets a method take a flexible number of arguments as a single parameter, usually as an array. When a parameter is marked with params, the caller can pass either individual values or an array. This makes it easier to call the method, especially when dealing with optional or unknown numbers of values.

For instance

void PrintParamsWithArray(params int[] numbers)
{
    foreach (var number in numbers)
    {
        Console.WriteLine(number);
    }
}
//The usage would be:
PrintParamsWithArray(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

Before C# 13, the params keyword could only be used with arrays. However, with this new feature, you can now use params with any collection type.

The recognized collection types are given below.

System.Span<T>, System.ReadOnlySpan<T>, and types that implement System.Collections.Generic.IEnumerable<T> and have an Add method. You can also use interfaces like System.Collections.Generic.IEnumerable<T>, System.Collections.Generic.IReadOnlyCollection<T>, System.Collections.Generic.IReadOnlyList<T>, System.Collections.Generic.ICollection<T>, and System.Collections.Generic.IList<T>, not just specific types.

Let us look at an example.

Use List with params.

internal static class WithList
{
    public static void Display(params List<int> numbers)
    {
        foreach (var number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}
// Usage
Console.WriteLine("Params with List");
WithList.Display([1, 2, 3, 4, 5, 6, 7]);

Use Span with params.

public static void Display(params ReadOnlySpan<int> numbers)
{
    foreach (var number in numbers)
    {
        Console.WriteLine(number);
    }
}
// Usage:
Console.WriteLine("Params with Span");
WithSpan.Display(1, 2, 3, 4, 5);

Let’s run the program, and you’ll see the following output.

The params keyword in C# is a useful tool for managing variable numbers of arguments. Since it was introduced in C# 1.0, it has helped developers write cleaner and more flexible code by making it easier to pass multiple parameters to methods.

Happy Coding!

Best ASP.NET Core 8.0.8 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 8.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment