.NET 4, do you know the new features? - Top 10 most useful features

Submitted by Robert MacLean on Thu, 09/08/2011 - 10:05


More in this series can be found in the introduction.

In the past two posts we have looked at the negative (top 10 least known, top 12 most useless) so lets switch to the positive side and see what is on the list for top 10 most useful features.

This is worked out by those who knew the feature and indicated it was useful using the same ratio as explained in the in top 12 most useless post.

CLR/BCL: IsNullOrWhiteSpace

Useful Ratio: 21 : 1

Introduction: We have had IsNullOrEmpty on string for a while but in .NET 4 we have the added IsNullOrWhiteSpace which checks for Nulls, empty strings or strings of just white space.

Thoughts: Easy to see why this is useful, it covers more scenarios than the old one, it is easily discoverable and it solves a difficult problem (I do not think many people understand all the white space charters).

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-ii-string

CLR/BCL: Stream.CopyTo

Useful Ratio: 19 : 1

Introduction: You have two streams, you want to copy from one stream to another one, which previously took 6 lines of code, a loop and 3 variables now uses 1 line of code and always works.

Thoughts: A common problem and a great solution and is easily discoverable because the method name is exactly what you would be looking for.

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-ix-stream

CLR/BCL: Enum.HasFlag

Useful Ratio: 17 : 1

Introduction: Enumerator has support for bitwise operations but previously has involved a fairly unintuitive calculation. I think that it was so unintuitive that some people have never even realised bitwise operations were supported. Now we have a single method which makes it VERY easy.

Thoughts: Making the hard easy and making it very discoverable – the factors of all these top useful functions!

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-iii-enum

CLR/BCL: Enum.TryParse

Useful Ratio: 7.6 : 1

Introduction: Going from a string to an enum has been possible in the past but it has never been a fluid experience. No support for generics and not easy to to handle bad data easily. TryParse solves those by brining generics into it and providing a response to tell you if it succeeds.

Thoughts: I think this is a BIG problem for many developers and this is a great and useful solution.

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-iii-enum

CLR/BCL: TimeSpan parsing improvements

Useful Ratio: 5.7 : 1

Introduction: Parsing strings into timespans is not easy, there is many ways to type in time ranges and it has been difficult for people to get right constantly… until .NET 4!

Thoughts: I am surprised at how highly this is up the list, it is useful but ranked this high surprises me as I do not think many people use TimeSpan’s in business systems.

Update: Mark Stacey on Twitter provided some good business use cases I didn't think of (Tweet 1, Tweet 2): "Absolutely. Business process stuff ~ loan applications, time since call was logged, tons of others. Especially where multiple applications work in process."

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-i-timespan

CLR/BCL: MemoryCache

Useful Ratio: 5 : 1

Introduction: MemoryCache is a per process in memory cache for your application which is VERY easy to use regardless of application type – prior to .NET 4 only ASP.NET had an in memory cache. There is also some low plumbing that makes it possible to have the cache stored to other locations, like files or SQL.

Thoughts: This is one of only two features in this list that also appeared in another list, in both cases the top 10 unknown features. This brings joy to me since it means that the issue here is discoverability because once you know it – you find big value in it Smile

More Info: http://www.sadev.co.za/content/net-baby-steps-part-vii-caching

CLR/BCL: string.Join

Useful Ratio: 4.7 : 1

Introduction: Join allows us to concatenate an array of strings together with a specific separator character in place.

Thoughts: This is very useful, as there is often times you need to loop over strings and build up another string. Think of in line SQL generation with a WHERE clause! Very useful stuff.

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-ii-string

Parallel: Parallel Extensions

Useful Ratio: 3.7 : 1

Introduction: Writing code that runs across cores has not been easy and the parallel extensions make it easy to understand how when thinking about normal for or foreach looping structures as it provides implementations of those looping structures which do run across multiple cores.

Thoughts: A fantastic edition to the framework and a much needed one to help solve the issue of having multi-core machines but the complexity of threading not being worth the effort. Why this is came in so high compared to the other two new additions in parallel (TPL was ranked 14th most useful & PLINQ 22nd) is odd but maybe because this is the easiest of the three to understand.

As a side not this is the only one of the most useful top 11 that I didn’t cover in my .NET 4 Baby Steps series which really makes me proud since it shows I was on target for that series.

More Info: http://www.sadev.co.za/content/pulled-apart-part-vii-plinq-not-easy-first-assumed

CLR/BCL: 64bit identification on Environment class

Useful Ratio: 3.5 : 1

Introduction: The environment class has been enhanced to have two new properties which help if the OS is 64bit and the process running is 64bit.

Thoughts: This also appeared on the top 10 unknown feature list, so really highlights that developers are either not thinking about 64bit at all but when they do, the tools they need are available.

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-xiii-tiny-steps


Useful Ratio: 3.2 : 1

Introduction: Lazy allows you to wrap a class (target class) in another class (Lazy<T> class) and gain lazy constructor calling on the target class.

Thoughts: Very nice feature, but I am surprised it did this well on the useful list as I see this as a bandage to a bad design solution. Proper use of patterns and planning in code should prevent usage – but I’ve been known to be wrong so tell me in the comments why you see this as useful!

More Info: http://www.sadev.co.za/content/net-4-baby-steps-part-v-lazy