Monday, September 9, 2013

Pet Peeve #1

Calling ToString() on an object that is already typed as a String.  I see this in all sorts of situations, but this morning it was when accessing a configuration value:
ConfigurationManager.AppSettings["EmailFrom"].ToString();

AppSettings is a NameValueCollection.  When accessing its this[string name] or this[int index] properties, it returns a String.  Don’t give future developers more code that they have to read and subsequently ignore, that doesn’t add to readability.

2 comments:

Muralidhara Raju Indukuri said...

Best information shared.
aws training in hyderabad

Realtime Experts said...

Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site
:Italian Training in Bangalore