C# Error CS0558 – User-defined operator must be declared static and public

C# Compiler Error

CS0558 – User-defined operator ‘{0}’ must be declared static and public

Reason for the Error

You will get this error in your C# code when you are performing an operator loading and have defined it as non-static or non-public.

For example, let’s compile the below C# program

using System;
using System.Diagnostics;

namespace DeveloperPublishConsoleCore
{
    public class CustomClass
    {
        // CS0558 
        public CustomClass operator +(CustomClass param1, CustomClass param2)    
        {
            return null;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0558 because you have an user-defined operator + inside the CustomClass as member that is non-static.

C# Error CS0558 – User-defined operator must be declared static and public

Error CS0558 User-defined operator ‘CustomClass.operator +(CustomClass, CustomClass)’ must be declared static and public DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 9 Active

Solution

You can fix this error in your C# program by changing the custom operator as static.

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