开发者

Why should I upgrade to c# 4.0?

I know there are some nice new features in C# 4.0 but I can't, for the life of me, think of a compelling reason for either upgrading existing projects or for switching to new projects.

I've seen some posts where people have said that if their hosting service didn't provide .Net 4 that they'd find another provider as .Net 4 was pinicle to their direction <?>.

Now开发者_运维百科 my boss is trying to get me to agree to switch all our production environments to C# 4 and to do it now.

So the question is has anyone either began using, or converted a project to, C# 4 for a compelling reason? Was there a feature that you just had to have that would make your life so much easier?


There are some cool new features in C# 4.0:

  • Dynamic member lookup
  • Covariant and contravariant generic type parameters
  • Optional ref Keyword when using COM
  • Optional parameters and named arguments
  • Indexed properties

In his release blog post Scott Guthrie goes into detail about the features of .NET 4 in general. Another great resource is a white paper at http://www.asp.net/learn/whitepapers/aspnet4. However, I'd doubt you are going to need one / any of these new features right away. As Scott Hanselman blogged:

there's a lot of stuff that's new and added in .NET 4, but not in that "overwhelming-I-need-to-relearn-everything" way.

Whether or not you should upgrade is therefore dependent on a variety of other factors. Some reasons that spring to mind:

  • Standardizing your development environment on a single platform VS2010 over VS2008.
  • Size of the .NET Framework is substantially reduced
  • Speed improvements if you are a Visual Studio Tools for Office developer

An open dialogue with your manager seems appropriate to understand his reasoning for the upgrade. I'd argue that because it's shiny isn't a compelling reason.

As a reference this dated Stack Overflow question "Why not upgrade to the latest .net framework" provides the inverse to your question.


  • Quite frankly System.Collections.Concurrent has made developing multi-threaded applications a breeze.

  • The new and improved System.Linq.Expressions makes writing dynamically compiled code seem like child's play.

  • The new named parameters feature means I can have big constructors and not get confused as to what each parameter is. Immutable objects are just that much easier.


Surprisingly not mentioned:

  • PLINQ
  • Task Parallel Library


Is your question specific to C# 4.0, or .NET 4.0?

In C# 4.0 there are only a couple of really nice new features. Covariance/contravariance is not useful all the time, but when you run into a need for it, it can really save a lot of pain. Optional method parameters can reduce a lot of ugly method overrides, and make certain method calls a lot cleaner. If you're using COM or IronPython or any of a few similar frameworks, the dynamic keyword can also be a real lifesaver.

.NET 4.0 in general has a ton of really interesting features across a variety of frameworks. Foreign Key support in Linq to Entities, for example, is making life a lot easier for us. A lot of people are really excited about POCO support. They also added support for some of the LINQ methods (e.g. Distinct) that were previously missing from the Entity Framework.

So it will really all boil down to which frameworks you're using and how you're using them, and how expensive it will be for you to make the switch.


First, what is compelling to me may mean nothing to you. Having said that, I would upgrade Visual Studio if budget allows. In fact, personally I think there is a huge career risk in staying with a company that doesn't keep your tools up to date. You will fall behind in your knowledge of the field without access to the latest tools.

As for converting all your projects just to convert them it seems like folly to me. Putting aside all the extra work distribution (and upgrading the machines to have .NET 4), you have to consider the chance that you will have something go wrong. (And if you are like me some things must be called from 3rd party programs using .NET 3.5 making them unable to convert.)

My first rule would be that nothing is converted unless you are working on it anyway. But I would seriously look to convert anything that could use improvement from either parallel code, or COM interop.

I do have a compelling project that was converted. I had a long running web method being called. In the version that exists now, I return from the method without knowing the results. Instead I gave the user a way to check later. By moving to a parallel foreach loop this works much better and I can let the user know if there were any errors.

The same project is also being converted to use RIA services which have greatly improved and reducing the amount of my own code.


I upgraded for the same reason everyone else did.

...so I can put it on my resume :)


If you're starting a new project today, it's probably best to start it on 4.0, since down the road you will have to migrate it at some point anyways (assuming it stays around long enough, older versions of .net will simply stop being supported).

C# 4 implies other things.. depending on your project... WCF 4, WPF 4, ASP.NET 4, MVC 2, Entity Framework 2, etc.. So don't just look at C# as the reason to change, you also have to look at the whole stack. If there's still nothing compelling, then staying where you're at is probably a wise choice.


If you're doing WPF / Silverlight, I would definitely recommend upgrading to Visual Studio 2010 (I know, you can write .NET 4.0 code without an IDE, but that's an edge case if ever there were one).

The multi monitor support is nifty but buggy. I spend a lot of time trying to get windows to refresh.

In terms of language, the COM interop (as @Gvs mentioned) is also vastly improved with the dynamic datatype and optional parameters.

UPDATE: Multiple monitor support is pretty rock solid with VS 2010 SP1.


If you can get your boss to pop for the $10,000+ Visual Studio Ultimate Edition, IntelliTrace is a compelling reason to upgrade your environment and justification enough the for investment.


COM integration is much easier with the dynamic datatype, and optional parameters.


For me there are two things:

  1. optional arguments -- because I am sick of polluting classes with X versions of the same method (overloading)
  2. dynamic keyword -- because the expressiveness of generics in C# is a joke, this way I can at least "write what I mean" without hoops, of course with execution speed penalty

The more compact the code (i.e. if you express the idea without addition "oh, how to avoid limitation Y of the language"), the better, because the code is much easier to maintain and it is harder to make a stupid (or worse) mistake.


There are no compelling stability or security reasons to switch. Shouldn't that be your boss's concern?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜