C# 2 和 3 之间的差异

发布于 2024-07-27 12:46:03 字数 142 浏览 5 评论 0 原文

我对 C# 或 .net 知之甚少,但我有兴趣了解它们。

令我感兴趣的一件事是我不断听到“C# 3 真的很棒”。
这是为什么? 与 C# 有什么区别 2. 区别仅在 C# 中还是在 .net 中也有?

提前致谢。

I know very little about C# or .net but I am interested in learning about them.

One thing that intrigues me is that I keep hearing that "C# 3 is really great".
Why is that? What is the difference from C# 2. Are the differences just in C# or in .net also?

Thanks in advance.

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

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

发布评论

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

评论(9

℡寂寞咖啡 2024-08-03 12:46:03

我有一篇关于此的小文章:Bluffer's Guide to C# 3。 显然,我的书中有更多详细信息,但这应该足以让您继续下去。 简而言之:

  • 自动实现的属性:

    public int Value { get;   放;   } 
      
  • 对象和集合初始值设定项:

    表单 form = 新表单 { Size = new Size(100, 100), 
                             控件 = { 新标签 { Text = "Hi" } } 
                           }; 
      列表<字符串>   字符串=新列表<字符串>;   { “你好呀” }; 
      
  • 隐式类型局部变量:

    var x = new Dictionary();   // x 仍然是静态类型 
      
  • 隐式类型数组:

    DoSomething(new[] { "hi", "there"});   // 创建一个字符串数组 
      
  • 匿名类型:

    var jon = new { Name = "Jon", Age = 33 }; 
      
  • Lambda 表达式(类似于匿名方法,但更短):

    Func<字符串, int>   lengthFunc = x =>;   x.长度; 
      
  • 表达式树:

    // 将逻辑表示为数据 
      表达式>   长度表达式 = x =>   x.长度; 
      
  • 扩展方法:(静态方法,其行为类似于第一个参数类型的实例方法)

    public static string Reverse(此字符串文本) 
      { 
          char[] chars = text.ToCharArray(); 
          Array.Reverse(chars); 
          返回新字符串(字符); 
      } 
    
      ... 
    
      字符串 hello = "olleh".Reverse(); 
      
  • 查询表达式:

    var query = 来自人中的人 
                  其中 person.Age >   18 
                  选择人员.姓名; 
      

I have a little article about this: the Bluffer's Guide to C# 3. Obviously there are more details in my book but it should be enough to get you going. In short:

  • Automatically implemented properties:

    public int Value { get; set; }
    
  • Object and collection initializers:

    Form form = new Form { Size = new Size(100, 100),
                           Controls = { new Label { Text = "Hi" } }
                         };
    List<string> strings = new List<string> { "Hi", "There" };
    
  • Implicitly typed local variables:

    var x = new Dictionary<string, int>(); // x is still statically typed
    
  • Implicitly typed arrays:

    DoSomething(new[] { "hi", "there"}); // Creates a string array
    
  • Anonymous types:

    var jon = new { Name = "Jon", Age = 33 };
    
  • Lambda expressions (like anonymous methods but shorter):

    Func<string, int> lengthFunc = x => x.Length;
    
  • Expression trees:

    // Representation of logic as data
    Expression<Func<string, int>> lengthExpression = x => x.Length;
    
  • Extension methods: (static methods which act like instance methods on the type of their first parameter)

    public static string Reverse(this string text)
    {
        char[] chars = text.ToCharArray();
        Array.Reverse(chars);
        return new string(chars);
    }
    
    ...
    
    string hello = "olleh".Reverse();
    
  • Query expressions:

    var query = from person in people
                where person.Age > 18
                select person.Name;
    
等风来 2024-08-03 12:46:03

You can read all about what was introduced in C# 3.0 in the Wikipedia article.

好听的两个字的网名 2024-08-03 12:46:03

这是一个太大的问题,回答您问题的最佳来源是 C# 深入了解,作者:乔恩·斯基特

This is a too big question, the best source to answer your question is C# in depth by Jon Skeet

云仙小弟 2024-08-03 12:46:03

C# 3.0 最突出的主题是数据,它是通过语言集成查询(LINQ)实现的。 添加了大多数其他语言功能(例如隐式类型局部变量、匿名类型、lambda 表达式和扩展方法)来支持 LINQ。 LINQ 是一种在代码中编写类似 SQL 语句来查询多种类型数据源的方法。 这样做的吸引人之处在于,您现在可以通过一种通用的方式编写代码来访问不同的数据源,这意味着您不必在每次 MS 引入新的数据访问技术或需要使用时完全重新学习轮子。第三方数据源。 由于大多数主要数据库供应商都以某种方式支持 LINQ,因此这种常见的数据查询方式的好处如今已经逐渐显现出来。

有些人喜欢 LINQ,有些人不喜欢。 我认为这是对语言的一个很好的补充,但您应该考虑自己的要求和情况,并就采用 C# 3.0 是否适合您做出明智的决定。

The most prominent theme of C# 3.0 was data, which is realized through Language Integrated Query (LINQ). Most of the other language features, such as implicitly typed local variables, anonymous types, lambda expressions, and extension methods were added to support LINQ. LINQ is a way to write SQL-like statements in your code to query multiple types of data sources. What is attractive about this is that you now have a common way to write code to access different data sources, meaning that you don't have to completely re-learn the wheel every time MS introduces a new data access technology or you need to use a 3rd party data source. Since most of the major database vendors are supporting LINQ in some way, the benefits of this common way to query data is being realized today.

Some people like LINQ and others don't. I'm in the camp that believes it is a great addition to the language, but you should look at your own requirements and situation and make an informed decision on whether adopting C# 3.0 is right for you.

2024-08-03 12:46:03

这里列出了 3.0 中的一些新内容。 很多很棒的东西。

Here's a list of some of the new stuff in 3.0. Lots of great stuff.

萌辣 2024-08-03 12:46:03

Linq、lambda、var keywork、目标树、匿名对象、较短的属性语法、对象初始值设定项、集合初始值设定项、扩展方法。

但 Lambda 和扩展方法是最重要的。 C# 3.0 非常好。

Linq, lambda, var keywork, desition trees, anonymous objects, shorter property syntax, object initializers, collection initializers, Extension Methods.

But Lambda and Extension Methods are the big ones. C# 3.0 is amazingly good.

定格我的天空 2024-08-03 12:46:03

差异确实存在于 C# 中,而不仅仅存在于 .NET 中。

有趣的是,通过 Visual Studio 2008 多目标,您可以在以前仅限于 C# 2.0 的 .NET 2.x 项目中使用 C# 3.0 中的大部分漂亮的东西。

你不会得到

  • 表达式树转换,
  • Linq 查询关键字,[Skeet 说你可以...]

,但你确实得到

  • Lamda 表达式 [Skeet 这么说]
  • 集合和对象初始值
  • 设定项隐式类型局部变量和匿名类型,即 var< /code>
  • 自动实现的属性和
  • 部分方法定义

因此,只需开始使用新的语法糖即可。

The differences are really in C# and not (just) in .NET.

The fun thing is, with Visual Studio 2008 multi-targeting you can use most of the nifty stuff from C# 3.0 in a .NET 2.x project previously limited to C# 2.0.

You don't get

  • Expression tree conversions,
  • Linq Query Keywords, [Skeet says you can...]

but you do get

  • Lamda expressions [so says Skeet]
  • Collection and Object Initializers
  • Implicitly typed local variables and Anonymous Types i.e. var
  • Auto-Implemented Properties and
  • Partial Method Definitions

So, just start using the new syntactic sugar.

櫻之舞 2024-08-03 12:46:03

关于此主题有很多文章:

请参阅:http://www.developer .com/net/csharp/article.php/3561756

There are plenty of articles on this topic:

See: http://www.developer.com/net/csharp/article.php/3561756

舟遥客 2024-08-03 12:46:03

C# 的演变如下所示:

  • 1.0:托管代码
  • 2.0:泛型
  • 3.0:LINQ
  • 4.0:动态编程
  • 5.0:异步编程

请参阅演示文稿中的幻灯片 5 C# 和 Visual Basic 的未来,作者:PDC 10 的 Anders Hejlsberg。

The evolution of C# looks like this:

  • 1.0: Managed code
  • 2.0: Generics
  • 3.0: LINQ
  • 4.0: Dynamic programming
  • 5.0: Asynchronous programming

See slide 5 in the presentation The Future of C# and Visual Basic by Anders Hejlsberg from PDC 10.

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