HomeCSharpC# Error CS0066 – ‘event’ : event property must have both add and remove accessors

C# Error CS0066 – ‘event’ : event property must have both add and remove accessors

C# Compiler Error

CS0066 – ‘event’ : event property must have both add and remove accessors

Reason for the Error

When you create an event in your C# class and forget to include the definitions for the add and remove methods, you will end up receiving this error.

For example, try compiling the below code snippet.

using System;
public delegate void EventHandler(object sender, int e);
public class DeveloperPublish
{
   public event EventHandler Click  
   {
   }

   public static void Main()
   {
   }
}

This will result with the compiler error CS0065: ‘DeveloperPublish.Click’: event property must have both add and remove accessors.

C# Error CS0066 – 'event' : event property must have both add and remove accessors

Solution

Include the definitions for add and remove method with-in your events. For example, you can add the add and remove method for the click event handler as shown below.

public delegate void EventHandler(object sender, int e);
public class DeveloperPublish
{
    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...