Languages
[Edit]
EN

C# - multiple parameters in method

7 points
Created by:
Nabila-Burnett
385

In this short article, we would like to show how to pass multiple arguments to function in C#.

Quick solution:

void DoSomething(params String[] argumnets) // <--- multiple arguments by using: params String[]
{
    // some logic here ...

    // int count = arguments.Length;
    //
    // argumnets[0]
    // argumnets[1]
    // argumnets[2]
    // ...
}


// Using:

DoSomething();
DoSomething("arg 1");
DoSomething("arg 1", "arg 2");
DoSomething("arg 1", "arg 2", "arg N");

 

1. Documentation

Simple case:

Definition syntax
ResultType MethodName(params ArgumentsType[] arguments)
{
    /* ... */
}
Calling syntax
ResultType result = MethodName(
    paramVariable1,
    paramVariable2,
    // ...
    paramVariableN
);
Description

By adding param keyword to function argument we are able to create function that accepts multiple arguments.

Using that construction it is required to:

  • use array type with that param argument,
  • put param argument always as last.

The number of total arguments depends on function calling.

Composed cases:

Definition syntax
ResultType MethodName(
    ArgumentType1 argument1,
    ArgumentType2 argument2,
    // ...
    ArgumentTypeN argumentN,
    params ArgumentsType[] arguments
) {
    /* ... */
}
Calling syntax
ResultType result = MethodName(
    variable1,      variable2,      variableN,       /* ... */
    paramVariable1, paramVariable2, paramVariableN   /* ... */
);

 

Practical example

In ths section you can see 2 example cases:

  • multiple arguments function,
  • normal arguments function extended with additional arguments.
using System;

public class Program
{
    public static void Main(String[] args)
	{
        DoSomething();
        DoSomething("arg 1", "arg 2");
        DoSomething("arg 1", "arg 2", "arg 3", "arg N");

        DoSomething(1, 2, 3);
		DoSomething(1, 2, 3, "arg 1", "arg 2");
		DoSomething(1, 2, 3, "arg 1", "arg 2", "arg 3", "arg N");
    }
	
	private static void DoSomething(params String[] arguments)
	{
		Console.WriteLine("Arguments count: " + arguments.Length);
	}
	
	private static void DoSomething(int a, int b, int c, params String[] arguments)
	{
		Console.Write("a=" + a + ", ");
		Console.Write("b=" + b + ", ");
		Console.Write("c=" + c + ", ");
		Console.WriteLine("Arguments count: " + arguments.Length);
	}
}

Output:

Arguments count: 0
Arguments count: 2
Arguments count: 4
a=1, b=2, c=3, Arguments count: 0
a=1, b=2, c=3, Arguments count: 2
a=1, b=2, c=3, Arguments count: 4

 

References

  1. params (C# Reference) - Microsoft Docs

Alternative titles

  1. C# - multiple arguments in method
  2. C# - multiple parameters in function
  3. C# - multiple arguments in function
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join