Created
May 15, 2024 12:10
-
-
Save danielskovli/a137836ce688a0c275ce819327e7eaf5 to your computer and use it in GitHub Desktop.
Action delegates as configuration objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | |
} | |
} |
Author
danielskovli
commented
May 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment