Params - Background
- Params was introduced in C# 1.0
params parameters have allowed developers to declare methods that accept a variable number of arguments, passed as an array. This feature simplifies method calls when the number of arguments is unknown at compile time - Single params per method: Only one params parameter in a method is allowed to avoid ambiguity. We need to specify the array type, and all arguments passed must be compatible with that type.
- Params must be the last parameter: The params keyword should be the last parameter in the method signature to ensure that the method correctly interprets the arguments.
- Empty array: If no arguments are passed to a params parameter, it will be treated as an empty array.
- Only one Params keyword is allowed and no additional Params will be allowed in function declaration after a params keyword.
- Simplifies Method Overloading,Code Readability is improved.
Params - Beyond Arrays
using System.Threading.Channels;
var calculate = new Calculate();
//without params
calculate.SimpleCalculate(1, 2, 3 );//without params...method overload
calculate.SimpleCalculate(1, 2, 3 ,4);//without params...method overload
//Collection expression - without params
calculate.SimpleCalculateCollectionExpression(new int[] { 1, 2, 3 }); //without collection expression...
calculate.SimpleCalculateCollectionExpression([1, 2, 3]); //with collection expression....c#12...
//with params...
calculate.SimpleCalculateParams(new int[] { 1, 2, 3 });
calculate.SimpleCalculateParams([1, 2, 3 ]);
calculate.SimpleCalculateParams(1, 2, 3 );
//what was not working before c#13 ??? This was not compiling......
//so before c#13 it worked only for arrays, but now it can work for any collection type..
calculate.SimpleCalculateParamsBeforeCsharp13(1, 2, 3);
/*-------------------------ANOTHER EXAMPLE---------------------------------------------------------*/
//This takes example of params with list of employees..which was not working earlier...now it works..
var emp = new EmployeeSalary();
emp.CalculateSalary([new Employee() { Salary = 1000 }, new Employee() { Salary = 2000 }]);
emp.CalculateSalary(new Employee() { Salary = 1000 }, new Employee() { Salary = 2000 });
/*-----------------------------------------------------------------------------------*/
public class Calculate
{
public void SimpleCalculate(int a, int b, int c) => Console.WriteLine(a + b + c);
public void SimpleCalculate(int a, int b, int c, int d) => Console.WriteLine(a + b + c + d);
public void SimpleCalculateParams(params int[] numbers) => Console.WriteLine(numbers.Sum());
public void SimpleCalculateCollectionExpression(int[] numbers) => Console.WriteLine(numbers.Sum());
public void SimpleCalculateParamsBeforeCsharp13(params List numbers) => Console.WriteLine(numbers.Sum());
}
public class EmployeeSalary
{
//public void CalculateSalary(params Employee[] employees)
//{
//}
/*----------works for list as well---------*/
public void CalculateSalary(params List employees)
{
Console.WriteLine(employees.Sum(emp => emp.Salary));
}
//public void CalculateSalary(params Span employees)
//{
// //Console.WriteLine(employees.Sum(emp => emp.Salary));
// foreach (var item in employees)
// {
// Console.WriteLine(item.Salary);
// }
//}
}
public class Employee
{
public string? Id { get; set; }
public int Salary { get; set; }
}
Params with span is deadly comobination
I will update this part of blog later..