C# 4.0 向后兼容 C# 2.0 吗?

发布于 2024-09-01 21:32:12 字数 120 浏览 5 评论 0原文

我可以知道 C# 4.0 和 C# 2.0 有什么区别吗? C# 4.0 向后兼容 C# 2.0 吗?

我可以说 C# 4.0 是 C# 2.0 的超集(就像 C++ 与 C 一样)吗?

谢谢。

May I know what is the difference between C# 4.0 and C# 2.0? Is C# 4.0 backward compatible to C# 2.0?

Can I say that C# 4.0 is a superset of C# 2.0 (just like what C++ is to C)?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾挽 2024-09-08 21:32:12

C# 4.0几乎向后兼容以前的版本,但有很少有重大变化。对于大多数普通代码,您不会注意到这些重大更改。

对于 C# 4 中的新功能,您可以查看 Wikipedia 文章,其中有相当多的内容很好的总结和例子。关键点是:

  • 动态成员查找
  • 协变和逆变泛型类型参数
  • 使用 COM 时可选的 ref 关键字 可选
  • 参数和命名参数
  • 索引属性

另请记住,中间还有另一个版本 - C# 3.0。这里最重要的添加之一是 LINQ 以及为此而添加的所有功能。 C# 2.0 和 3.0 之间的差异比 C# 3.0 和 4.0 之间的差异大得多。


顺便说一句,正如您在问题中暗示的那样,并非所有有效的 C 代码都是有效的 C++。请参阅此处

从严格的数学意义上来说,C 不是 C++ 的子集。有些程序是有效的 C,但不是有效的 C++,甚至有几种编写代码的方法在 C 和 C++ 中具有不同的含义。

C# 4.0 is nearly backwards compatible with previous versions but there are a few breaking changes. For most ordinary code you won't notice these breaking changes.

For the new features in C# 4 you can check out the Wikipedia article which has quite a good summary with examples. The key points are:

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

Also remember that there was another version in between - C# 3.0. One of the most important additions here is LINQ and all the features added to make it possible. The difference between C# 2.0 and 3.0 is much greater than the difference between C# 3.0 and 4.0.


By the way, not all valid C code is valid C++ as you implied in your question. See here:

In the strict mathematical sense, C isn't a subset of C++. There are programs that are valid C but not valid C++ and even a few ways of writing code that has a different meaning in C and C++.

疑心病 2024-09-08 21:32:12

与之前的版本一样,有一些微妙的重大变化。问题在于,C# 4 由于协变/逆变引入了一种新的有效转换类型。这意味着一些以前适用的方法现在适用于特定的调用。下面是一些对 C# 4 和 C# 2 / 3 都有效的代码:

using System;
using System.Collections.Generic;

public class Base
{
    public void DoSomething(IEnumerable<string> strings)
    {
        Console.WriteLine("Base");
    }
}

public class Derived : Base
{
    public void DoSomething(IEnumerable<object> objects)
    {
        Console.WriteLine("Derived");
    }
}

public class Test
{
    static void Main()
    {
        Derived d = new Derived();
        d.DoSomething(new List<string>());
    }
}

在 C# 4 中,这将打印“Derived” - 在 C# 2 和 3 中,它将打印“Base”。

C# 1 和 2 之间也出现了同样的问题,其中委托实例表达式允许非泛型协变和逆变。

任何新的转换都必然会产生此类问题。然而,根据我的经验,这些事情不太可能真正引起问题。

此外,C# 4 以稍微不同的方式处理锁定和类似字段的事件 - 同样,这不会影响大多数代码,但值得了解。 Chris Burrows 在其博客中发布了关于这些更改的系列文章

There are some subtle breaking changes, as there have been in previous versions. The problem is that C# 4 introduces a new type of valid conversion due to covariance/contravariance. This means that some methods which would previously have not been applicable are now applicable for a particular call. Here's some code which is valid for both C# 4 and C# 2 / 3:

using System;
using System.Collections.Generic;

public class Base
{
    public void DoSomething(IEnumerable<string> strings)
    {
        Console.WriteLine("Base");
    }
}

public class Derived : Base
{
    public void DoSomething(IEnumerable<object> objects)
    {
        Console.WriteLine("Derived");
    }
}

public class Test
{
    static void Main()
    {
        Derived d = new Derived();
        d.DoSomething(new List<string>());
    }
}

In C# 4, this will print "Derived" - in C# 2 and 3 it will print "Base".

The same sort of issue occurred between C# 1 and 2, where delegate instance expressions allowed nongeneric covariance and contravariance.

Any new conversions are pretty much bound to create this sort of issue. In my experience, however, these things are unlikely to actually cause a problem.

Additionally, C# 4 handles locking and field-like events in a slightly different way - again, this won't affect most code, but it's worth knowing about. Chris Burrows has a series of articles on the changes in his blog.

晚风撩人 2024-09-08 21:32:12

是的,他们过去常常这样做。

C# 4.0 中的新增功能

Yes, they used to do it like that.

Whats new in C# 4.0

遇到 2024-09-08 21:32:12

是的,

C# 2.0 的所有功能仍然保留在 C# 4.0 中,但是,版本 4 中的新功能显然在版本 2 中没有。

您在 C# 2.0 中编写的所有内容都可以在 C# 4.0 中使用。

但显然相反的做法是行不通的......

YES,

All the features of C# 2.0 are in still in C# 4.0, HOWEVER, there are new features in version 4 that are obviously not in version 2.

Everything you wrote in C# 2.0 will work in C# 4.0.

But obviously the opposite would not work.....

装迷糊 2024-09-08 21:32:12

C# 2.0 添加了许多功能,如下所示:

所以请查看上面的链接以了解新功能..

There are many featured added from C# 2.0 as follow :

so please review the above links to know the new features ..

酒解孤独 2024-09-08 21:32:12

不过,请注意 - 留意 thinks 被弃用 - 例如 ADO.Net 中的 OracleClient...

Martin

编辑 - 是的,Simon 说得有道理...我真的在谈论 .Net。对不起...

A word of caution though - watch out for thinks getting deprecated - OracleClient in ADO.Net for instance...

Martin

EDIT - Yeah, Simon's got a point there... I'm really talking about .Net. Sorry...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文