© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•11mo ago•
6 replies
SWEETPONY

✅ How to make static field required to use from interface?

I have this interface:
public interface IEventProcessor
{
    public static IReadOnlyCollection<string> HandledEvents { get; } = [];
}
public interface IEventProcessor
{
    public static IReadOnlyCollection<string> HandledEvents { get; } = [];
}


HandledEvents
HandledEvents
is static because I can easily read values from it using reflection and without
Activator.CreateInstance
Activator.CreateInstance


Processor example:
public sealed class FlightEventProcessor(JsonSerializerOptions serializerOptions): IEventProcessor
{
    public static string[] HandledEvents => ["FlightAdded", "FlightUpdated"];
}
public sealed class FlightEventProcessor(JsonSerializerOptions serializerOptions): IEventProcessor
{
    public static string[] HandledEvents => ["FlightAdded", "FlightUpdated"];
}


the main problem H
andledEvents
andledEvents
can be easily removed from
FlightEventProcessor
FlightEventProcessor
and nothing will happens. compilator allows me to not implement this array inside every classes that inherit
IEventProcessor
IEventProcessor
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Static interface methods
C#CC# / help
3y ago
How to understand pining static field, const and instance field
C#CC# / help
2y ago
Static Members of an Interface
C#CC# / help
7mo ago
✅ Can I use static abstract interface members in netstandard2.1?
C#CC# / help
4y ago