Skip to content

Instantly share code, notes, and snippets.

@MelbourneDeveloper
Last active February 14, 2021 01:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MelbourneDeveloper/1eaf53ffc5944ab3eb4f339bde4ca306 to your computer and use it in GitHub Desktop.
Save MelbourneDeveloper/1eaf53ffc5944ab3eb4f339bde4ca306 to your computer and use it in GitHub Desktop.
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