Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielskovli/a137836ce688a0c275ce819327e7eaf5 to your computer and use it in GitHub Desktop.
Save danielskovli/a137836ce688a0c275ce819327e7eaf5 to your computer and use it in GitHub Desktop.
Action delegates as configuration objects
using System;
var configuredWhatever = ConfigurationTest(x =>
{
x.Option1 = "Overwritten by caller";
x.Option2 = "Provided by caller";
});
Console.WriteLine(configuredWhatever);
Whatever ConfigurationTest(Action<Whatever> configureWhatever)
{
var whatever = new Whatever { Option1 = "Provided by method" };
configureWhatever.Invoke(whatever);
return whatever;
}
class Whatever
{
public string HasDefaultValue { get; set; } = "The default value";
public string Option1 { get; set; }
public string Option2 { get; set; }
public override string ToString()
{
return $"HasDefaultValue={HasDefaultValue}, Option1={Option1}, Option2={Option2}";
}
}
@danielskovli
Copy link
Author

HasDefaultValue=The default value, Option1=Overwritten by caller, Option2=Provided by caller

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment