简体   繁体   English

为什么要升级到C#4.0?

[英]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. 我知道C#4.0中有一些不错的新功能,但是我一生都无法想到升级现有项目或切换到新项目的迫切原因。

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 <?> . 我见过一些帖子,人们说过,如果他们的托管服务未提供.Net 4,则他们会找到另一个提供商,因为.Net 4符合他们的发展方向<?>

Now my boss is trying to get me to agree to switch all our production environments to C# 4 and to do it now. 现在,我的老板正试图让我同意将我们所有的生产环境切换到C#4并立即执行。

So the question is has anyone either began using, or converted a project to, C# 4 for a compelling reason? 那么问题来了,是否有人出于令人信服的原因开始使用C#4或将项目转换为C#4? 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 : C#4.0中有一些很酷的新功能

  • Dynamic member lookup 动态成员查找
  • Covariant and contravariant generic type parameters 协变和逆变泛型类型参数
  • Optional ref Keyword when using COM 使用COM时的可选ref关键字
  • 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 . Scott Guthrie在其发布的博客文章中详细介绍了.NET 4的一般功能 Another great resource is a white paper at http://www.asp.net/learn/whitepapers/aspnet4 . 另一个很棒的资源是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 : 正如Scott Hanselman所说:

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. .NET 4中有很多新内容,但不是以“压倒性的我需要重新学习所有内容”的方式。

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. 相对于VS2008,在单个平台VS2010上标准化您的开发环境。
  • Size of the .NET Framework is substantially reduced .NET Framework的大小已大大减少
  • Speed improvements if you are a Visual Studio Tools for Office developer 如果您是Office开发人员的Visual Studio工具,则可以提高速度

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. 作为参考,这个过时的Stack Overflow问题“ 为什么不升级到最新的.net框架 ”提供了与您的问题相反的结论。

  • Quite frankly System.Collections.Concurrent has made developing multi-threaded applications a breeze. 坦率地说, System.Collections.Concurrent使开发多线程应用程序变得轻而易举。

  • The new and improved System.Linq.Expressions makes writing dynamically compiled code seem like child's play. 新的和改进的System.Linq.Expressions使编写动态编译的代码看起来像是在玩耍。

  • 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: 出人意料的是没有提到:

Is your question specific to C# 4.0, or .NET 4.0? 您的问题仅针对C#4.0或.NET 4.0吗?

In C# 4.0 there are only a couple of really nice new features. 在C#4.0中,只有几个非常好的新功能。 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. 如果您使用的是COM或IronPython或任何其他类似框架,则dynamic关键字也可以是真正的救星。

.NET 4.0 in general has a ton of really interesting features across a variety of frameworks. 总体而言,.NET 4.0在各种框架中都有大量真正有趣的功能。 Foreign Key support in Linq to Entities, for example, is making life a lot easier for us. 例如,Linq to Entities中的外键支持使我们的生活变得更加轻松。 A lot of people are really excited about POCO support. 很多人对POCO的支持感到非常兴奋。 They also added support for some of the LINQ methods (eg Distinct ) that were previously missing from the Entity Framework. 他们还增加了对某些LINQ方法(例如Distinct )的支持,这些方法以前是实体框架中缺少的。

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. 话虽如此,如果预算允许,我将升级Visual Studio。 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. 撇开所有额外的工作分配(并将机器升级为.NET 4),您必须考虑出现问题的机会。 (And if you are like me some things must be called from 3rd party programs using .NET 3.5 making them unable to convert.) (如果您像我一样,则必须使用.NET 3.5从第三方程序调用某些操作,从而使它们无法转换。)

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. 但是我会认真地考虑转换任何可以使用并行代码或COM互操作进行改进的东西。

I do have a compelling project that was converted. 我确实有一个引人注目的项目,该项目已被转换。 I had a long running web method being called. 我有一个运行时间长的Web方法被调用。 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. 通过转到并行的foreach循环,效果会更好,我可以让用户知道是否有任何错误。

The same project is also being converted to use RIA services which have greatly improved and reducing the amount of my own code. 同样的项目也正在转换为使用RIA服务,该服务已大大改善并减少了我自己的代码量。

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). 如果您今天开始一个新项目,最好在4.0上启动它,因为在将来,您无论如何都必须迁移它(假设它保存的时间足够长,.net的较早版本将不再被支持的)。

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. C#4包含其他内容..取决于您的项目... WCF 4,WPF 4,ASP.NET 4,MVC 2,实体框架2等。因此,不要只是将C#视为更改的原因,您还必须查看整个堆栈。 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). 如果您正在执行WPF / Silverlight,我绝对建议您升级到Visual Studio 2010(我知道,您可以在没有IDE的情况下编写.NET 4.0代码,但是如果有的话,这是一个极端的情况)。

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. 在语言方面,COM互操作(如@Gvs所述)也通过动态数据类型和可选参数得到了极大的改进。

UPDATE: Multiple monitor support is pretty rock solid with VS 2010 SP1 . 更新: VS 2010 SP1对多监视器的支持非常牢固。

如果您可以让老板抢购价格超过10,000美元的Visual Studio Ultimate Edition,则IntelliTrace是升级环境和合理性足以吸引投资的诱人理由。

使用dynamic数据类型和optional参数,COM集成要容易得多。

For me there are two things: 对我来说,有两件事:

  1. optional arguments -- because I am sick of polluting classes with X versions of the same method (overloading) 可选参数-因为我讨厌用相同方法的X版本污染类(重载)
  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 动态关键字-因为C#中泛型的表达是个玩笑,因此我至少可以“毫不费力”地“写出我的意思”,当然这会降低执行速度

The more compact the code (ie 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. 代码越紧凑(即,如果您在不加“哦,如何避免限制语言Y”的情况下表达想法),则越好,因为代码易于维护,并且变得更愚蠢(或更糟)。 )错误。

There are no compelling stability or security reasons to switch. 没有令人信服的稳定性或安全性理由要切换。 Shouldn't that be your boss's concern? 那不是你老板的事吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM