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()
    {
    }
}
Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Hey, wait!Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.