Home »

What are the new 2.0 features useful for?

Question ListCategory: ASP.NETWhat are the new 2.0 features useful for?
alisataylore190 author asked 8 years ago
1 Answers
jully882 author answered 8 years ago

Generics are useful for writing efficient type-independent code, particularlywhere the types might include value types. The obvious application is
container classes, and the .NET 2.0 class library includes a suite of generic
container classes in the System.Collections.Generic namespace. Here’s a
simple example of a generic container class being used:
List myList = new List();
myList.Add( 10 );
Anonymous methods reduce the amount of code you have to write when
using delegates, and are therefore especially useful for GUI programming.
Here’s an example
AppDomain.CurrentDomain.ProcessExit += delegate { Console.WriteLine(“Process ending
…”); };
Partial classes is a useful feature for separating machine-generated code
from hand-written code in the same class, and will therefore be heavily used
by development tools such as Visual Studio.
Iterators reduce the amount of code you need to write to implement
IEnumerable/IEnumerator. Here’s some sample code:
Satish Marwat Dot Net Web Resources satishcm@gmail.com 31 Page
static void Main()
{
RandomEnumerator re = new RandomEnumerator( 5 );
foreach( double r in re )
Console.WriteLine( r );
Console.Read();
}
class RandomEnumerator : IEnumerable
{
public RandomEnumerator(int size) { m_size = size; }
public IEnumerator GetEnumerator()
{
Random rand = new Random();
for( int i=0; i < m_size; i++ ) yield return rand.NextDouble(); } int m_size = 0; } The use of 'yield return' is rather strange at first sight. It effectively synthethises an implementation of IEnumerator, something we had to do manually in .NET 1.x.

Please login or Register to Submit Answer