C# Error CS0226 – An __arglist expression may only appear inside of a call or new expression

C# Compiler Error

CS0226 – An __arglist expression may only appear inside of a call or new expression.

Reason for the Error

You will receive this error in your C# program when you have used the keyword __arglist outside of the method call or new expression.

__arglist in C# is used to send parameters to a method. We usually send parameters to a function in C# by specifying the parameters in the method declaration and definition. If we have to pass a new set of arguments, we will have to either modify the method definition or use method overloading.

For example, try to compile the below code snippet.

using System;

namespace DeveloperPubNamespace
{   
    class Program
    {
        public static void Main()
        {
            __arglist(1, 2, 3, 4, "Senthil Balu");
        }
    }
}

This program will result with the error code CS0226 because we are using __arglist as if we are calling a method. Usually, we need to call the __arglist inside a method call.

Error CS0226 An __arglist expression may only appear inside of a call or new expression ConsoleApp3 C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 14 Active

C# Error CS0226 – An __arglist expression may only appear inside of a call or new expression

Solution

To fix the error code CS0226 in C#, you’ll need to ensure that you use __arglist inside the method call or new expression as shown below.

using System;
namespace DeveloperPubNamespace
{   
    class Program
    {
        public static int Length(__arglist)
        {
            ArgIterator iterator = new ArgIterator(__arglist);
            return iterator.GetRemainingCount();
        }
        public static void Main()
        {           
            int result = Length(__arglist(1, 2, 3, 4, "Senthil Balu"));
            Console.WriteLine(result);
        }
    }
}

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...