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

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