What are C# Method Parameters

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

C# Programming Tutorials

A method in C# consists of a block of code that can accept arguments, be reused, and return a value. In this programming tutorial, we will take a look at method parameters in C# with relevant code examples and use cases.

Read: The Top Task Management Software for Developers

What is a Method in C#?

In software development, a method is a block of code that executes a predefined set of instructions. A method can have an access modifier, a return type, a name, and any arguments the method can accept when invoked. This is also known as the method signature.

The most common types of access modifiers are public and private. While public methods can be called from outside the class or struct in which they are defined, a private method can only be invoked within the container class or struct for security reasons.

The return type of a method defines the type of data returned by the method when executed. Methods that return no value have a void return type. If the return type of a method is not void, developers should explicitly mention the return type in the method signature.

What are C# Method Parameters?

In C#, method parameters are the variables that are passed as arguments to a method when it is invoked. Method parameters are declared within the method’s parentheses after the method’s name. Method parameters are optional, i.e., you can have a method with or without any parameters.

Method parameters can also be set up to use a parameter array, allowing for an infinite number of parameters to be used when calling the method. Additionally, certain types of parameters can be set up as output parameters, which allow for information to be sent out from a method back to the calling code.

For example, a method may need an integer value to perform some calculation. The integer value can be passed as a parameter to the method. You can have another method that accepts a double as an argument and performs some calculation using the value.

How to Pass Parameters to Methods

In C#, a method parameter is an input variable of a method. A parameter can be of any type, including value types, reference types, and output parameters. When you call a method, you must supply a value for each parameter. It should be noted that the order and type of the arguments passed when calling a method should be identical to the order and type of parameters defined in the method signature.

The following C# example code shows a simple method named AddIntegers that accepts two integer values as parameters, adds the values, and then returns the sum of the two numbers:

public int AddIntegers(int x, int y)
{
   return (x + y);
}

Let’s enclose the AddIntegers method inside a class:

    public class Demo
    {
        public int AddIntegers(int x, int y)
        {
            return (x + y);
        }
    }

You can now use the following piece of code to invoke the AddIntegers method:

    static void Main(string[] args)
    {
       Demo obj = new Demo();
       obj.AddIntegers(5, 10);
       Console.Read();
    }

Read: Best Online Courses to Learn C#

Named Parameters in C#

Named parameters allow programmers to pass values to the parameters of a method by referring to the names of the parameters. You may or may not pass the parameters in the order in which they are defined in the method signature. Here is an example of how to use named parameters in C#:

Demo obj = new Demo();
obj.AddIntegers(y:10, x:5);

Out Parameters in C#

You can use the out keyword in C# to define out parameters in a method. It should be noted that out parameters are generally used when you want to return multiple values from a method.

The code example below shows how you can define an out variable as a method parameter in C#:

public void AddIntegers(int x, int y, out int sum)
{
    sum = x + y;
}

You can now call the AddIntegers method using the following code:

Demo obj = new Demo();
obj.AddIntegers(7, 8, out int sum);
Console.WriteLine(sum);

Ref Parameters in C#

You can pass a variable by reference using the ref keyword in the method signature. Here is a code example showing how to use the ref keyword in C#:

public string GetText(ref string text)
{
    return text.ToUpper();
}

You can pass a value by reference as shown in the code given below:

Demo obj = new Demo();
string str = "Hello World!";
obj.GetText(ref str);

Default or Optional Parameters in C#

You can pass parameters to a method in C# optionally if default or optional parameters have been defined:

public int AddIntegers(int x, int y=0)
{
    return (x + y);
}

In this example, the value of y has been set to 0. So, if developers do not pass any value to y when calling the method, the default value will be used:

obj.AddIntegers(5); //The value of y is 0 by default

Using Params Array as Parameter

Programmers can also pass any number of values as parameter to a method in C#. The following code shows how you can define a method that can accept variable numbers of parameters using a params array in C#:

public int AddIntegers(params int[] arr)
{
   int sum = 0;

   for(int i=0; i<arr.Length; i++)
   {
       sum += arr[i];  
   }
  return sum;
}

The following code shows how you can pass multiple parameters when calling the AddIntegers method:

Demo obj = new Demo();
int sum = obj.AddIntegers(1,2,3,4,5);
Console.WriteLine(sum);

Final Thoughts on C# Method Parameters

If a method is overloaded, each overloaded version of the method must have a unique signature. That is, two methods cannot differ only by return type. This allows calling code to unambiguously identify which overloaded version of a method it intends to call.

You can learn more about method overloading in our tutorial: How to Overload Methods in C#.

Joydip Kanjilal
Joydip Kanjilal
A Microsoft Most Valuable Professional in ASP.NET, Speaker, and Author of several books and articles. More than 25 years of experience in IT with more than 18 years in Microsoft .NET and its related technologies. He was selected as a Community Credit Winner at http://www.community-credit.com several times. He has authored 8 books and more than 500 articles in some of the most reputed sites worldwide including MSDN, Info World, CodeMag, Tech Beacon, Tech Target, Developer, CodeGuru, and more.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read