Caffeine-Powered Life

Why Delegates?

The more code I write, the more I find myself using delegates. Take this part of our Curiosity.Common.Mvc library, just added today. Behold, the power of delayed execution.

return new FormHandlerResult<Logon>(form, () => RedirectToAction("Index", "Home"), () => View(form))
{
    SuccessNotification = () => "Welcome, " + Session["display_name"]
};

I have a dynamic success notification in this case. The form handler will update the session with the display name, but it’s not set yet. If I were to just use a string for success notification, I couldn’t set the string at a later time. In this case, using a Func solves all of my problems.

Comments