How to Resolve Error "CS0068: 'event': event in interface cannot have initializer" in C#
The Compiler Error CS0068 is an interface definition error. The message reads: "'event': event in interface cannot have initializer".
In C#, an Interface defines a contract (a specific set of public members) that a class must implement. It defines what a class can do, but traditionally (and specifically regarding state), it does not define how it does it. Adding an initializer (assigning a value like = null or = delegate {}) implies creating a storage field and assigning an initial state to it. Interfaces are not allowed to store instance state, so this syntax is forbidden.
This guide explains why this restriction exists and how to correctly implement initialized events in the classes that use the interface.
Understanding Interfaces and State
When you declare an event in a class, you can initialize it to avoid null checks:
// In a CLASS, this is valid:
public event EventHandler MyEvent = delegate { };
However, an Interface contains only the signatures of members. It does not contain the backing fields required to store the delegate assignment. When you try to assign a value inside the interface, you are trying to add implementation logic and data storage to a purely abstract contract.
Scenario: Attempting to Initialize in the Interface
A common pattern in C# is to initialize an event to an empty delegate so you can raise it without checking for null. Developers often try to apply this pattern directly to the interface definition to enforce it globally, but this triggers CS0068.
Example of Error:
using System;
public interface INotifier
{
// ⛔️ Error CS0068: 'INotifier.SomethingHappened': event in interface cannot have initializer
// An interface cannot store the "= delegate { }" logic or state.
event EventHandler SomethingHappened = delegate { };
}
Solution: Initialize in the Implementing Class
The interface should only declare that the event exists. The responsibility of initializing that event (assigning a default value) belongs to the Class that implements the interface.
Step 1: Clean up the Interface
Remove the assignment part from the interface.
using System;
public interface INotifier
{
// ✅ Correct: Only the signature is defined here.
event EventHandler SomethingHappened;
}
Step 2: Initialize in the Class
Implement the interface in a class, and apply your initialization logic there.
public class EmailNotifier : INotifier
{
// ✅ Correct: The class holds the state, so it can have an initializer.
// We assign an empty delegate to ensure this is never null.
public event EventHandler SomethingHappened = delegate { };
public void DoWork()
{
Console.WriteLine("Work done.");
// Because of the initialization above, we don't need '?.Invoke'
// (Though '?.Invoke' is still the modern best practice).
SomethingHappened(this, EventArgs.Empty);
}
}
C# 8.0 Default Interface Methods:
While modern C# allows interfaces to have default method implementations (void Method() { ... }), they still cannot have instance fields or instance event initializers. The rule enforcing CS0068 remains strict because interfaces cannot hold state (data).
Conclusion
CS0068 reminds you of the separation of concerns in C#:
- Interfaces define the contract (Signature only).
- Classes provide the implementation (Storage and Logic).
To fix this error, simply remove the = ... assignment from your interface definition and move it to the class implementing that interface.