Compiler Error CS0079
The event 'event' can only appear on the left hand side of += or -=
An event was called incorrectly. For more information, see Events and Delegates.
The following sample generates CS0079:
C#
// CS0079.cs
using System;
public delegate void MyEventHandler();
public class Class1
{
private MyEventHandler _e;
public event MyEventHandler Pow
{
add
{
_e += value;
Console.WriteLine("in add accessor");
}
remove
{
_e -= value;
Console.WriteLine("in remove accessor");
}
}
public void Handler()
{
}
public void Fire()
{
if (_e != null)
{
Pow(); // CS0079
// try the following line instead
// _e();
}
}
public static void Main()
{
Class1 p = new Class1();
p.Pow += new MyEventHandler(p.Handler);
p._e();
p.Pow += new MyEventHandler(p.Handler);
p._e();
p._e -= new MyEventHandler(p.Handler);
if (p._e != null)
{
p._e();
}
p.Pow -= new MyEventHandler(p.Handler);
if (p._e != null)
{
p._e();
}
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
.NET feedback
.NET is an open source project. Select a link to provide feedback: