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
Next Post
Process Improvement  25 Feb 2010
Prior Post
Validation shouldn't be a Blunt Instrument  18 Feb 2010
Related Posts
Superpowers for your AI  29 Mar 2026
Autotitling Windows Terminal Tabs  17 Mar 2026
Don't assume shared understanding  25 Jan 2026
Better Table Tests in Go  21 Oct 2025
Error assertions  26 Apr 2025
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
Archives
February 2010
2010