C# Error CS0243 – The Conditional attribute is not valid on ‘method’ because it is an override method

C# Compiler Error

CS0243 – The Conditional attribute is not valid on ‘method’ because it is an override method

Reason for the Error

You will receive this error in your C# program when you have used the Conditional Attribute on a method that is marked as override.

For example, try compiling the below code snippet.

namespace DeveloperPubNamespace
{
    public class BaseClass
    {
        public virtual void Method1() { }
    }

    public class ChildClass : BaseClass
    {
        [System.Diagnostics.ConditionalAttribute("DeveloperPub")] 
        public override void Method1() { }
    }

    class Program
    {
        public static void Main()
        {
        }
    }
}

This program will result with the C# error code CS0243 because the ConditionalAttribute on the Method1 that is marked as override in the ChildClass.

Error CS0243 The Conditional attribute is not valid on ‘ChildClass.Method1()’ because it is an override method DeveloperPublish C:\Users\SenthilBalu\source\repos\ConsoleApp3\ConsoleApp3\Program.cs 10 Active

C# Error CS0243 – The Conditional attribute is not valid on 'method' because it is an override method

Solution

The C# compiler will never bind the override methods. You can only bind the base method. Hence the ConditionalAttribute is not allowed on the override methods in C#.

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