Is C# 4.0 backward compatible to C# 2.0?

痞子三分冷 提交于 2019-11-30 09:24:57
Mark Byers

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++.

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.

Yes, they used to do it like that.

Whats new in 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.....

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

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

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...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!