Skip to content

Instantly share code, notes, and snippets.

@mcihad
Forked from karenpayneoregon/BirthDay.cs
Created September 13, 2024 20:26
Show Gist options
  • Save mcihad/58c1b6782c45074733dab1b5c069141e to your computer and use it in GitHub Desktop.
Save mcihad/58c1b6782c45074733dab1b5c069141e to your computer and use it in GitHub Desktop.
public class BirthDay
{
private const string Template =
"""
Hey,
{name}'s birthday is on {dob}, which is in {month}.
Let's plan a party!
""";
private readonly Dictionary<string, string> _parameters = new();
private BirthDay(string name, DateOnly dob)
{
_parameters.Add(@"{name}", name);
_parameters.Add(@"{dob}", $"{dob:MM/dd/yyyy}");
_parameters.Add(@"{month}", $"{dob:MMMM}");
}
public static BirthDay CreateInstance(string name, DateOnly dob) => new(name, dob);
public override string ToString()
=> _parameters.Aggregate(Template, (sender, kv)
=> sender.Replace(kv.Key, kv.Value));
}
var result = BirthDay.CreateInstance("Mary", new DateOnly(1956, 9, 21)).ToString();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment