Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Null Object Example
#nullable enable
namespace NullObjectExample
{
class Program
{
static void Main()
{
var someService = new SomeService();
}
}
public class SomeService
{
private readonly IPerformsAction performsAction;
public SomeService(IPerformsAction? performsAction = null)
{
this.performsAction = performsAction ?? NullActionPerformer.Instance;
}
}
public interface IPerformsAction
{
void PerformAction();
}
public class NullActionPerformer : IPerformsAction
{
public static NullActionPerformer Instance { get; } = new NullActionPerformer();
public void PerformAction()
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment