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.