views:

215

answers:

5

Relating to another question I asked yesterday with regards to logging I was introduced to TraceListeners which I'd never come across before and sorely wish I had. I can't count the amount of times I've written loggers needlessly to do this and nobody had ever pointed this out or asked my why I didn't use the built in tools. This leads me to wonder what other features I've overlooked and written into my applications needlessly because of features of .NET that I'm unaware of.

Does anyone else have features of .NET that would've completely changed the way they wrote applications or components of their applications had they only known that .NET already had a built in means of supporting it?

It would be handy if other developers posted scenarios where they frequently come across components or blocks of code that are completely needless in hindsight had the original developer only known of a built in .NET component - such as the TraceListeners that I previously noted.

This doesn't necessarily include newly added features of 3.5 per se, but could if pertinent to the scenario.

Edit - As per previous comments, I'm not really interested in the "Hidden Features" of the language which I agree have been documented before - I'm looking for often overlooked framework components that through my own (or the original developer's) ignorance have written/rewritten their own components/classes/methods needlessly.

+3  A: 

You should take a look at this post

http://stackoverflow.com/questions/9033/hidden-features-of-c

jasonco
I wasn't so much looking for hidden features, it's just that the .NET framework is so vast that I was looking for commonly overlooked features... such as the TraceListener
BenAlabaster
I already reviewed that post and it covers a lot of what I consider the language basics, but doesn't cover commonly overlooked components of the framework itself.
BenAlabaster
Oh I see... that post could help anyways. Good Luck.
jasonco
+2  A: 

Also:

Joel Coehoorn
Most of these are really only covering language features, not framework components. There is a real nice SMTP feature listed that's very useful though, and a link to http://dotnettipoftheday.org/tips/ which has been added to my favourites list.
BenAlabaster
+5  A: 

The yield keyword changed the way I wrote code. It is an AMAZING little keyword that has a ton of implications for writing really great code.

Yield creates "differed invoke" with the data that allows you to string together several operations, but only ever traverse the list once. In the following example, with yield, you would only ever create one list, and traverse the data set once.

FindAllData().Filter("keyword").Transform(MyTransform).ToList()

The yield keyword is what the majority of LINQ extensions were built off of that gives you the performance that LINQ has.

Brian Genisio
Exactly. Now I'm thinking more in terms of enumerables instead of lists, which can sometimes simplify code a great deal.
petr k.
It's really more the IEnumerable interface: you _can_ build efficient IEnumerables without yield without too much trouble.
Joel Coehoorn
+1  A: 

The most frequently overlooked feature I came across is the ASP.net Health Monitoring system. A decent overview is here: http://aspnet.4guysfromrolla.com/articles/031407-1.aspx

I know I personally recreated it on several apps before I actually saw anything in a book or on the web about it.

I spoke to someone at a conference one time and asked about it. They told me the developer at MS had bad communication skills so it was largely left undocumented :)

rifferte
lol @ the bad communication skills causing poor documentation. I think that's frequently the biggest issue.
BenAlabaster
+1  A: 

I re-wrote the System.Net.WebClient class a while back. I was doing some web scraping and started my own class to wrap HttpWebRequest/HttpWebReponse. Then I discovered WebClient part way through. I finished it anyway because I needed functionality that WebClient does not provide (control of cookies and user agent).

Something I'm thinking about re-writing is the String.Format() method. I want to reflect the code used to parse the input string and mimic it to build my own "CompiledFormat" class that you can use in a loop without having to re-parse your format string with each iteration. The result will allow efficient code like this:

var PhoneFormat = new CompiledFormat("({0}){1}-{2}x{3}");
foreach (PhoneNumber item in MyPhoneList)
{
    Console.WriteLine(PhoneFormat.Apply(PhoneNumber.AreaCode, PhoneNumber.Prefix, PhoneNumber.Number, PhoneNumber.Extension));
}

Update:
This prompted me to finally go do it. See the results here: http://stackoverflow.com/questions/761121/performance-issue-building-a-string

Joel Coehoorn
The CompiledFormat sounds intriguing, can you elaborate because I frequently create my unformatted string and then pass it into a bunch of String.Format() instances where I want to use different params... is that what you're getting at?
BenAlabaster
Kind of. I'll edit my question to elaborate.
Joel Coehoorn
Neat, seems like an extremely useful application of the concept. +1
BenAlabaster