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.
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()
{
}
}
