Language: C#
Write out expression and its results in one call.
/// <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())); }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

