I'm not sure if this is going to boil down to a matter of opinion, but I keep seeing a pattern in code that I don't understand.
All over C# apps, I keep seeing the expression:
System.Configuration.ConfigurationManager.AppSettings["key"].ToString()
For a long time I thought it was just a quirk where I work, but I searched it, and it appears it's not just a few one-off things. People do it a fair amount, but it isn't really talked about as far as I can see.
This blog, for example, makes sure to call .ToString()
on AppSettings, and so does this blog
Moreover, all the programmers on this question make sure to call .ToString()
on the AppSettings collection.
To make sure I'm not going crazy, I rolled over the method in Visual Studio, which assures me that, yes, AppSettings[key]
is strongly typed as a string and that "no actual conversion is performed" when calling .ToString()
.
It would seem to me the only real difference between AppSettings[key]
and AppSettings[key].ToString()
is the possibility of throwing a NullReferenceException.
So my question is:
Is there any technical reason for doing this, or is it a widely-recommended C# best practice for some reason, or is it just a weird quirk with no real meaning?