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

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