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.

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