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

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