Menu

Tuesday, 4 February 2025

 

.NET 9 Features

  1. Task.WhenEach in .NET 9
  2. CountBy LINQ in .NET 9
  3. AggregateBy LINQ in .NET 9
  4. Index LINQ in .NET 9
  5. UUID v7 in .NET9
  6. SearchValues improvements in .NET9
  7. Semi Auto Properties in .NET9
  8. Params Collection in .NET9

Params Collections in .NET9

 

About


The series of features exploring in .NET 9 posts, This post, we are focusing on "Params Collections" feature in .NET9. Please refer the following links for other .NET 9 features discussed in my earlier posts.

Introduction


Params collection is one of the feature in C#13. This params parameter can be of any types supported for collection parameters.
This enhanced feature helps to write code in cleaner way and it also improves in performance point of view as well.
Lets explore and understand about this with sample code by comparing before .NET9 and now.

Before .NET 9

 //Before .NET 9
 public static void PrintNumbers( params int[] numbers)
 {
     Console.WriteLine("Numbers in Array: " + string.Join(", ", numbers));
 }
 public static void PrintNumbers(List numbers)
 {
     Console.WriteLine("Numbers in List: " + string.Join(", ", numbers));
 }

.NET 9

 // .NET 9
public static void PrintNumbersInNET9(params ReadOnlySpan numbers)
{
    Console.WriteLine("Numbers in ReadOnlySpan: " + string.Join(", ", numbers.ToArray()));
}

public static void PrintNumbersInNET9(params IEnumerable numbers)
{
    Console.WriteLine("Numbers in IEnumerable: " + string.Join(", ", numbers));
}

Call those methods  just like below.


// Using params with a Array or List
var numbersList = new List { 6, 7, 8 };
ParamsCollection.PrintNumbers(numbersList.ToArray());
ParamsCollection.PrintNumbers(numbersList);

// Using params with ReadOnlySpan
ParamsCollection.PrintNumbersInNET9([6, 7, 8]);

// Using params with List
ParamsCollection.PrintNumbersInNET9(numbersList.Where(x => x > 6));

The aforesaid scenario where you have two method overloads: one accepting params IEnumerable<T> and another taking params ReadOnlySpan<T>. The method resolution works as follows:

  • When passing a sequence of values directly, the params ReadOnlySpan<T> overload is chosen.
  • If an array is provided as an argument, the params ReadOnlySpan<T> overload is also selected, since arrays can be implicitly converted to ReadOnlySpan<T>.
  • However, when a List<T> is passed, the params IEnumerable<T> overload is preferred, as List<T> implements IEnumerable<T> but does not have an implicit conversion to ReadOnlySpan<T>.

Code Screenshots








Summary


Params collection feature provides these following benefits.
Cleaner code: It reduces the number of calls to .ToArray() and .ToList()
Performance: Methods like .ToArray() and .ToList() inherently introduce additional resource overhead. However, the updated implementation now supports passing Span<> and IEnumerable<>, optimizing memory usage and enabling lazy execution. This enhancement improves efficiency while offering greater flexibility for performance-critical scenarios.

Thanks for your reading and as always share your feedback on this.