.NET 4, do you know the new features? - Top 10 most useful features
More in this series can be found in the introduction.
In the past two posts, we’ve looked at the negative (top 10 least known, top 12 most useless), so let’s switch to the positive side and see what’s on the list for the 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 top 12 most useless post.
CLR/BCL: IsNullOrWhiteSpace
Useful Ratio: 21 : 1
Introduction: We’ve had IsNullOrEmpty on strings for a while, but in .NET 4, we now have the added IsNullOrWhiteSpace, which checks for nulls, empty strings, or strings composed solely of whitespace.
Thoughts: It’s easy to see why this is useful—it covers more scenarios than the old one, is easily discoverable, and solves a tricky problem (I don’t think many people understand all the whitespace characters).
More Info: https://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, and you want to copy from one to another. Previously, this took 6 lines of code, a loop, and 3 variables—now it’s just one line and always works.
Thoughts: A common problem with a great solution, and it’s easily discoverable because the method name is exactly what you’d be looking for.
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-ix-stream
| CLR/BCL: Enum.HasFlag |
|---|
| Useful Ratio: 17 : 1 |
Introduction: Enums have supported bitwise operations, but doing so previously involved a fairly unintuitive calculation. I suspect many developers didn’t even realize bitwise operations were supported. Now, we have a single method that makes it very easy.
Thoughts: Making the hard easy—and making it very discoverable—are hallmarks of these top useful functions!
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-iii-enum
| CLR/BCL: Enum.TryParse |
|---|
| Useful Ratio: 7.6 : 1 |
Introduction: Converting a string to an enum has always been possible, but never a seamless experience. There was no support for generics, and handling bad data wasn’t straightforward. TryParse fixes those issues by introducing generics and providing a clear success/failure response.
Thoughts: This is a big problem for many developers, and TryParse provides a great solution.
More Info: https://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 TimeSpan objects has always been tricky—there are many ways to format time ranges, and developers struggled to get it right… until .NET 4!
Thoughts: I’m surprised this ranks so highly, as I don’t think many business systems rely on TimeSpan. However, Mark Stacey on Twitter shared some compelling use cases I hadn’t considered (Tweet 1, Tweet 2):
"Absolutely. Business process stuff—loan applications, time since a call was logged, tons of others. Especially where multiple applications work in process."
More Info: https://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—extremely easy to use, regardless of application type. Before .NET 4, only ASP.NET had an in-memory cache. There’s also underlying support for caching to other locations (files, SQL, etc.).
Thoughts: This is one of two features on the list that also appeared in another list (top 10 unknown features). That brings me joy—it means the real issue is discoverability, because once you know it, you love it! 😊
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-vii-caching
| CLR/BCL: string.Join |
|---|
| Useful Ratio: 4.7 : 1 |
Introduction: Join lets us concatenate an array of strings with a specific separator in one go.
Thoughts: This is very useful—there are often cases where you need to loop over strings and build another string. Think inline SQL generation with a WHERE clause!
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-ii-string
| Parallel: Parallel Extensions |
|---|
| Useful Ratio: 3.7 : 1 |
Introduction: Writing code that leverages multiple cores hasn’t been easy, but Parallel Extensions make it intuitive by providing parallel implementations of familiar loops (for, foreach). Now, you can write multithreaded code without the complexity.
Thoughts: A fantastic addition to the framework—and a much-needed one. Multi-core machines are everywhere, but threading complexity has often made them impractical. Why this ranks higher than the other two parallel features (TPL at 14th, PLINQ at 22nd) is odd—maybe because this is the easiest of the three to understand.
Side note: This is the only one of the top 11 most useful features I didn’t cover in my .NET 4 Baby Steps series, which makes me proud—it means I was on target for that series.
More Info: https://www.sadev.co.za/content/pulled-apart-part-vii-plinq-not-easy-first-assumed
CLR/BCL: 64-bit identification on Environment class |
|---|
| Useful Ratio: 3.5 : 1 |
Introduction: The Environment class has been enhanced with two new properties to help determine whether:
- The OS is 64-bit, and
- The process running is 64-bit.
Thoughts: This also appeared on the top 10 unknown features list, highlighting that developers either don’t think about 64-bit or, when they do, need better tooling.
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-xiii-tiny-steps
CLR/BCL: Lazy<T> |
|---|
| Useful Ratio: 3.2 : 1 |
Introduction: Lazy<T> lets you wrap a class (the target) in another class (Lazy<T>) to enable lazy construction of the target.
Thoughts: A nice feature, but I’m surprised it ranks this highly—I see it as a bandage for poor design. Proper patterns and planning should prevent the need for it. (But I’ve been wrong before—tell me in the comments why you find this useful!)
More Info: https://www.sadev.co.za/content/net-4-baby-steps-part-v-lazy