C# Error CS0231 – A params parameter must be the last parameter in a formal parameter list

C# Compiler Error

CS0231 – A params parameter must be the last parameter in a formal parameter list

Reason for the Error

You will receive this error if you have multiple parameters in your C# program and the an parameter of type params exists before other parameters.

For example, try to compile the below code snippet.

namespace DeveloperPubNamespace
{
    class Program
    {
        // This will return the C# error code CS0231
        public void ProcessData(params int[] param1, int param2) 
        {
        } 

        public static void Main()
        {
        }
    }
}

This program will result with the C# error code CS0231 because the function ProcessData has the first parameter with params keyword and the second parameter of type int.

Error CS0231 A params parameter must be the last parameter in a formal parameter list DeveloperPublish C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 6 Active

Solution

To fix the error code CS0231 in C#, you’ll need to ensure that the arguments with the params keyword always appear at the end of the argument list for the method as shown below.

namespace DeveloperPubNamespace
{
    class Program
    {
        // This will return the C# error code CS0231
        public void ProcessData(int param2, params int[] param1) 
        {
        } 

        public static void Main()
        {
        }
    }
}

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...