C# Error CS0070 – The event ‘event’ can only appear on the left hand side of += or -=

C# Compiler Error

CS0070 – The event ‘event’ can only appear on the left hand side of += or -= (except when used from within the type ‘type’)

Reason for the Error

You will receive this error when you have an event and try assigning it without an add or subtract references outside of where the event class is defined.

For example, try compiling the below code snippet.

public delegate void EventHandler();
public class classA
{
    public event EventHandler Click;

    public static void OnClick()
    {
        EventHandler eventHandler;
        classA a = new classA();
        eventHandler = a.Click;
    }
   
}

public class DeveloperPublish
{
    public int Foo()
    {
        EventHandler eh = new EventHandler(classA.OnClick);
        classA a = new classA();
        eh = a.Click;  

        return 1;
    }
    public static void Main()
    {
    }
}

This will result with the compiler error

Error CS0070 The event ‘classA.Click’ can only appear on the left hand side of += or -= (except when used from within the type ‘classA’)

C# Error CS0070 – The event 'event' can only appear on the left hand side of += or -= (except when used from within the type 'type')

Solution

Change the code eh = a.Click to include the += or -= as shown below to fix this error.

C# Error CS0070 – The event 'event' can only appear on the left hand side of += or -= (except when used from within the type 'type')

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