C# Params

WBOY
WBOYOriginal
2024-09-03 15:19:00287browse

Params are a very important keyword in the C#. We used param when we wanted to give the number of arguments as the variable. So it is used when the developer does not know the number of parameters that will be used. After the C# Params keyword, no additional param will be allowed in the function. If we do not pass any arguments, then the length of params will remain zero. We can send comma-separated values or arrays.

Keyword: params

using System;
namespace Examples {
class Test {
// function containing params parameters
public static int Addittion(params int[] ListNumbers)
{
int total = 0;
// foreach loop
foreach(int i in ListNumbers)
{
total += i;
}
return total;
}
// Driver Code
static void Main(string[] args)
{
// Calling function by passing 5
// arguments as follows
int y = Addittion (12,13,10,15,56);
// Displaying result
Console.WriteLine(y);
}
}
}

Output:

C# Params

With params Keyword:

static public int add(params int[] args)
{
int add1 = 0;
foreach (var item in args)
add1= add1+item + 2;
return add1;
}

Without params Keyword:

static public int add(int[] args)
{
int add = 0;
foreach (var item in args)
add1 = add1+item + 2;
return add1;
}

With param, we can call the method like add(1,4,5), but without param, we cant.

Examples of C# Params

The following examples show how we can implement params in C#.

Example #1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params int[] num) // Params Paramater
{
for (int a = 0; a < num.Length; a++)
{
Console.WriteLine(num[a]);
}
}
static void Main(string[] args) // main function
{
Demo program = new Demo();  // Creating Object
program.Show(20, 4, 64, 3, 20, 2, 14);  // Passing arguments of variable length
Console.ReadLine();
}
}
}

In the above example, a param keyword is used, allowing any type and number of types. Then, after creating the object, we pass several arguments to display.

Output:

C# Params

Example #2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public void Show(params object[] val) // Params Paramater
{
for (int a = 0; a < val.Length; a++)
{
Console.WriteLine(val[a]);
}
}
static void Main(string[] args) // main method
{
Demo program = new Demo(); // Creating Object
program.Show("Javascript", "Dotnet", 11, 10.50, "Param", 'h',"Example"); // Passing arguments of variable length
Console.ReadLine();
}
}
}

In the above example, a param keyword is used, allowing any type and number of types. After creating an object, we pass several arguments to display. Here you can see the arguments of the different data types.

Output:

C# Params

Example #3

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
public static int Addition(params int[] num) // params parameter
{
int add = 0;
// foreach loop
foreach (int a in num)
{
add += a;
}
return add;
}
static void Main(string[] args)
{
int x = Addition(23, 45, 2, 36, 76);  // call function
// Displaying result
Console.WriteLine(x);
Console.ReadLine();
}
}
}

In the above example, an array is used, and there is no need to mention the size of an array as a param keyword is used, which will allow any type and number of the argument.  The numbers will be in the following format.

[0] 25
[1] 45
[2]  2
[3]  36
[4]  76

Output:

C# Params

Example #4

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Params
{
class Demo
{
static void Main()
{
// Call params method with five integer type arguments
int cal1 = calParameters(7);
int cal2 = calParameters(11, 23);
int cal3 = calParameters(46, 8, 45);
int cal4 = calParameters(23, 31, 21, 45);
int cal5 = calParameters(12, 12, 54, 76);
// display result of each calculations
Console.WriteLine(cal1);
Console.WriteLine(cal2);
Console.WriteLine(cal3);
Console.WriteLine(cal4);
Console.WriteLine(cal5);
Console.ReadLine();
}
static int calParameters(params int[] values)
{
int sum = 0;
foreach (int value in values)    // foreach loop and sum of integers
{
sum += value;
}
return sum;
}
}
}

There are five arguments for which a param keyword is used in the above example. All the arguments are of integer type. A foreach loop is used to display the sum of each argument.

Output:

C# Params

How is it Different From an Array?

public void test(params int[] args) { }
test(); // no compile-time error, args will be empty

But if You Use an Array:

public void test(int[] args) { }
test(); // error: No overload for 'Foo' takes 0 arguments

So, If we do not pass any arguments, then the length of params will remain zero. Param keyword must be the last in the parameter list; otherwise, it will give an error.

Example:

public void test(params int[] args,int value) { }

This declaration is not allowed.

The above is the detailed content of C# Params. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# HashSetNext article:C# HashSet