CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

Write out expression and its results in one call.

228 Views
Copy Code Show/Hide Line Numbers
/// <summary>
/// Writes the text (expression body) of the function being called, followed by its output.
/// </summary>
/// <typeparam name="T">The return type of expression/function.</typeparam>
/// <param name="expression">
/// The expression/function to be evaluated and whose output will be displayed.
/// </param>
/// <remarks>
/// Useful in seeing the results of function calls while debugging or 
/// working on spike solutions (see http://www.extremeprogramming.org/rules/spike.html). 
/// NOTE: the results are displayed using the ToString() of the returned type.
/// </remarks>
///  <example>
/// // Testing DateTime/TimeSpan extension methods
/// Echo(() => 45.Days().Ago().ToShortDateString());
/// 
/// // Output assuming today is July 4th, 2009:
/// 45.Days().Ago().ToShortDateString(): 5/20/2009
/// </example>
static void Echo<T>(Expression<Func<T>> expression)
{
    Console.WriteLine(string.Format("{0}: {1}"
        , expression.Body
        , expression.Compile().Invoke()));
}
by Al Gonzalez
  July 14, 2009 @ 4:31pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate