Overview of Features Added in C# 2.0 and C# 3.0 Since its initial release in late 2000, the C# language has evolved considerably. This evolution has likely been accelerated thanks to the wide adoption of C#. With the release of Visual Studio 2005 and the .NET Framework 2.0, the C# compiler supported the C# 2.0 enhancements to the language. This was great news, since C# 2.0 included some handy features that provided a more natural programming experience as well as greater efficiency. 39925
Arguably, the meatiest addition to C# 2.0 was support for generics. The syntax is similar to C++ templates, but the main difference is that constructed types created from .NET generics are dynamic in nature—that is, they are bound and constructed at run time. This differs from C++ concrete types created from templates, which are static in the sense that they are bound and created at compile time.
Generics are most useful when used with container types such as vectors, lists, and hash tables, where they provide the greatest efficiency gains. Generics can treat the types that they contain specifically by their type, rather than by using the base type of all objects, System.Object.
C# anonymous methods are extremely useful with delegates and events. Delegates and events are constructs used to register callback methods that are called when triggered. Normally, you wire them up to a defined method somewhere. But with anonymous methods, you can define the delegate’s or event’s code inline, at the point where the delegate or event is set up. This is handy if your delegate merely needs to perform some small amount of work for which an entire method definition would be overkill. What’s even better is that the anonymous method body has access to all variables that are in scope at the point it is defined.Lambda expressions, which are new to C# 3.0, supersede anonymous methods and make for more readable code.
C# 2.0 added support for iterators. Anyone familiar with the C++ Standard Template Library (STL) knows about iterators and their usefulness. In C#, you typically use the foreachstatement to iterate over an object that behaves as a collection.
Finally, C# 2.0 added support for partial types. Prior to C# 2.0, you had to define each C# classentirely in one file (also called a compilation unit). This requirement was relaxed with the support for partial types. This was great news for those who rely upon code generators to provide skeleton code. For example, you can use the Visual Studio wizards to generate such useful things as System.Data.DataSetderived types for accessing data in a database. Prior to C# 2.0, it was problematic if you needed to make modifications to the generated code. You either had to derive from or contain the generated type in a new type while specializing its implementation, or you had to edit the generated code. Editing the generated code was risky because you normally lost those changes when the wizard was forced to regenerate the type for some reason. Partial types solve this problem, because now you can augment the generated code in a separate file so that your changes aren’t lost when the wizard regenerates the code. For a great example of how partial types are used, look at the code automatically generated when you create a Windows Forms application using Visual Studio.
C# 3.0 includes some great new features. Most of the new features are stepping stones designed to support Language Integrated Query (LINQ). Nevertheless, all of them are extremely useful when used inpidually outside of the context of LINQ. Many of them allow programmers to employ functional programming techniques more easily.
C# now supports implicitly typed local variables by making use of a new keyword var. It’s important to note that these variables are not typeless; rather, their type is inferred at compile time.
Auto-implemented properties are another helpful new feature to save us some typing and reduce the potential to introduce bugs. How many times have you simply declared a class to hold a few pieces of data and been annoyed with the amount of typing required to create property accessors for that data? After all, doing so follows good encapsulation practices. 对C# 2.0和C# 3.0的新功能概述英文文献和中文翻译:http://www.751com.cn/fanyi/lunwen_40653.html