C# Error CS0578 – The Conditional attribute is not valid on ‘function’ because its return type is not void

C# Compiler Error

CS0578 – The Conditional attribute is not valid on ‘function’ because its return type is not void

Reason for the Error

You will get this error in your C# code when you apply an Conditional attribute to a method without an return type (e.g. : void).

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

using System;

namespace DeveloperPublishConsoleCore
{
    internal class Program
    {
        // CS0578
        [System.Diagnostics.ConditionalAttribute("test")]
        public int GetWebsiteData()
        {
            return 0;
        }
        static void Main(string[] args)
        {
   

            Console.WriteLine("DeveloperPublish Hello World!");
        }
    }
}

You will receive the error code CS0578 because you have an method GetWebsiteData() which has an return type of void and you have used the conditional attribute on it.

C# Error CS0578 – The Conditional attribute is not valid on 'function' because its return type is not void

Error CS0578 The Conditional attribute is not valid on ‘Program.GetWebsiteData()’ because its return type is not void DeveloperPublishConsoleCore C:\Users\senth\source\repos\DeveloperPublishConsoleCore\DeveloperPublishConsoleCore\Program.cs 8 Active

Solution

C# doesn’t allow you to have a conditional attribute on a function/method with return type of void.

You can fix this error in your C# program by either deleting the conditional attribute or change the return type of the function from void.