C# Error CS0073 – An add or remove accessor must have a body

C# Compiler Error

CS0073 – An add or remove accessor must have a body

Reason for the Error

You will usually receive this error when you don’t specify the body for the add or remove definitions of your event.

Let’s have a look at the below code snippet.

delegate void EventHandler();
class Test
{
    public event EventHandler Click
    {
        add; 
        remove
        {
            Click -= value;
        }

    }

    public static void Main()
    {
    }
}

When you build the above C# code, you’ll receive the below error.

Error – CS0073 An add or remove accessor must have a body

C# Error CS0073 – An add or remove accessor must have a body

This is because the event Click has an add definition that doesnot have the body.

Solution

To fix the error CS0073, simply include the body for the add or remove definitions for the event. You can fix the above error with the below code snippet.

delegate void EventHandler();
class Test
{
    public event EventHandler Click
    {
        add
        {
            Click += value;
        }
        remove
        {
            Click -= value;
        }

    }

    public static void Main()
    {
    }
}

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