HomeCSharpC# Error CS0590 – User-defined operators cannot return void

C# Error CS0590 – User-defined operators cannot return void

C# Compiler Error

CS0590 – User-defined operators cannot return void

Reason for the Error

You will get this error in your C# code when you are performing an operator loading or defining an operator that does not return any value.

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

using System;
using System.Diagnostics;

namespace DeveloperPublishConsoleCore
{
    public class CustomClass
    {
        // CS0590 
        public void operator +(CustomClass param1, CustomClass param2)    
        {
        }
    }

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

You will receive the error code CS0590 because you have an user-defined operator + inside the CustomClass which doesnot contain any return value.

C# Error CS0590 – User-defined operators cannot return void

Error CS0590 User-defined operators cannot return void DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 9 Active

Solution

C# doesn’t allow you to have an user-defined operators without returning any value.

You can fix this error in your C# program by ensuring that return an object. For example

Leave a Reply

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