Courtesy of a tweet from Scott Hanselman, here’s a nifty ToString() extension method that allows you to name properties and fields directly.

It works like this:

var p
    = new Person
    {
        Name = "William Williams",
        BirthDate = new DateTime(1936, 1, 1)
    };

Console.WriteLine(p.ToString("{Name}, born {BirthDate:d}, Age {Age}"));

See the original on pastie.org.

Comments

blog comments powered by Disqus