Monday, February 15, 2016

Behold the Power of Mutation!

"And – ere a man hath power to say ‘ Behold!’ –"
-- Shakespeare, A Midsummer Night's Dream
Act I, Scene I, Line 147

One of the things I found very interesting in the Pluralsight class "Exploring C# 6 with Jon Skeet" was that the new interpolated strings works with expressions.  Since C# is a mutable language we can easily abuse this!



In the above C# REPL we see that we can we can have all kinds of fun as long as we have an expression in our interpolated string.

Example:

var intValue = 0;
var stringValue = $"{((Func)(() => { intValue++; return intValue; }))()}"

As long as the expression has a ToString value it will work.  Note, stringValue will only increment intValue once and after that it will always give the same result, but it is fun nonetheless.

Remember, never under estimate the power to abuse mutable languages!