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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...