A Look at Optional Parameters, Named Parameters and Permanent Redirect in .NET 4.0
While getting acquainted with VS 2010 and .NET 4.0, I’ve come across some improvements that that I’ll be sharing in this blog. Three features in particular are Optional Parameters, Named Parameters and Permanent Redirect.
Before .NET 4.0 one would have to overload methods if they wanted the ability to implement optional parameters. With this latest version of .NET, developers simply have to add the optional parameter in the last position of parameters as displayed below.
Another new feature in .NET 4.0 is named parameters, which allow you to ignore the parameter order and mention parameters with names in a different order. For example:
Although Microsoft coins this as an improvement, I find that this gives way for confusing code that may be hard to manage. For instance, imagine reading several lines of code, written by someone else, with methods having parameters all jumbled in no particular order.
Finally, in situations where we want to redirect users to a new page permanently and update the search engine’s indexes, a new command Response.RedirectPermanent can be used in .NET 4.0. In previous versions of .NET, Response.Redirect was used to do the redirection. The caveat in the aforementioned approach was that the header information for the page would not be updated, therefore allowing search engines to keep the old information of the page in their indexes for search. With the new command Response.RedirectPermanent, header information is updated on the server, the user is directly redirected to the new page and it saves a double trip to server as well. In addition, search engines, on re-indexing of that site and page, update the header information in their index for better performance on searches.