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

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