The new features of C#.NET are classified into four categories:- Generics, Iterators, Anonymous methods and Partial Classes.
Generics were introduced to enable developers create type safe data structures. They can be defined as utility helper classes that do not commit the developer to the actual data types being used. This feature improves performance and results in higher quality code as the algorithms can be reused without duplicating type specific code. They are an extension to the CLR type system and allow developers to define types which are unspecified. They are then specified at runtime as the code references the generic type and fills in the details, tailoring the type to particular needs. Though generics serve a similar purpose as C++ templates in managed code they have some additional features which include absences of code bloat and developer confusion. Four distinct benefits accrue to the developer at compile time—Type safety, binary code reuse, performance and clarity.
Iterators are used to iterate over data structures such as arrays, collections using a foreach loop. The condition being that the GetEnumerator method is implemented to return an IEnumerator interface. C# 2.0 defines the generic type safe IEnumerable and IEnumerator interfaces in System.Collections.Generics namespace to overcome these problems. There is no need to work with a design pattern. A single typed method called GetEnumerator() is sufficient. The values will be returned directly. The new keyword yield replaces the return keyword. The compiler then converts the new implementation into a enumerator pattern and eases the process of development. It avoids unnecessary coding and keeps the source code short. A significant feature is that any method can be made enumerable. The method has to return a value defined by the IEnumerable and use the yield keyword to return a single value.
C#.NET 2.0 supports delegates as means of invoking one or more methods. The Delegates provide operators and methods for adding or removing target methods and are frequently used in .NET Framework for events, callbacks, asynchronous calls and multithreading. Anonymous methods are a new feature in C#.NET 2.0 that lets the developer define an anonymous method called by a delegate. This method is defined inline and is not a member method of any class and there is no way of applying method attributes to it. The anonymous method cannot define generic types or add generic constraints. The compiler has to infer the type of delegate used, instantiate a new delegate object of the inferred type, wrap the new delegate around the anonymous method and assign it to the delegate used in the definition of the anonymous method. Anonymous methods can be used anywhere and can be passed to any method that accepts the appropriate delegate type as a parameter. Anonymous methods can use generic types as parameters.
Partial Classes are classes whose definition and implementation are split into two or more separate files. Every part of the class is marked with the new modifier partial. The compiler then looks for the marked and parts and merges the component parts and merges them into one complete implementation at runtime. A precondition is that the assembly of the code parts must be in the same project and a parallel compilation. The classes also should be matched logically and must have identical modifiers, supported interfaces etc. All attributes assigned at class level are handled automatically. This feature was directed towards rapid application development and probably for distinguishing between generated and custom code.
No comments:
Post a Comment