Sie sind auf Seite 1von 4

Some New features have been added to C# language with version 2.0 of .NET.

which are:

 Generics
 Iterators
 Anonymous methods
 Partial classes

Generics
We are going to take a look at a new cool feature of C# 2.0. It is not constrained to C# but also
available with other .NET languages.

As the projects get more complicated, programmers increasingly need a means to better reuse
and customize their existing component-based software. To achieve such a high level of code
reuse in other languages, programmers typically employ a feature called Generics. Using
Generics, we can create class templates that support any type. When we instantiate that class, we
specify the type we want to use, and from that point on, our object is "locked in" to the type we
chose.

Let us look at an example of how we can create a generic class using C# 2.0.

public​ ​class​ MyCustomList<MYTYPE>


{
private​ ArrayList m_list = ​new​ ArrayList();
public​ ​int​ Add(myType value)
{
return​ m_list.Add(value);
}

public​ ​void​ Remove(myType value)


{
m_list.Remove(value);
}

public​ myType ​this​[​int​ index]


{
get
{
return​ (myType)m_list[index];
}
set
{
m_list[index] = value;
}
}
}

Here, ​MyCustomList​ is built on an ​ArrayList​. But its methods and indexer are strongly typed.
Here ​<myType>​ is really just a placeholder for whatever type you choose when you create the
class. This placeholder is defined in angled brackets after the name of the class. Now, let us look
at how to create an instance of the class ​MyCustomList​:

MyCustomList<​int​> list = ​new​ MyCustomList<​int​>();


// Add two integers to list
list.Add(​1​);
list.Add(​33​);
// The next statement will fail and wont compile
list.Add(​"Emp"​);

If we want our ​MyCustomList​ to store strings, it can be done as follows:

MyCustomList<​string​> list = ​new​ MyCustomList<​string​>();

We can have multiple types in our base template class. Just separate each type with a comma in
between the angular brackets.

public​ ​class​ MySecondCustomList<myType,mySecondType>


{...}

So far so good.

Constraints

Now, we will look at applying constraints by which we can restrict what types are allowed. For
e.g., if we want to restrict our type to only the types implementing ​IDisposable​ interface, we
can do it as follows:

public​ ​class​ MyCustomList<myType>


where myType : IDisposable
{ ... }

If there are more than one constraints, we can separate it by commas. E.g.:

public​ ​class​ MyDictionary<KeyType, ValType> where


KeyType : IComparable,
KeyType : IEnumerable

Generics are not limited to classes. They can also be used in structures, interfaces, and delegates.
We can even use generics to parameterize a method by type. E.g.:

public​ myType MyGenericFunction<myType>(myType item)


{
........
return​ item;
}

Conclusion
Iterators
Iterators allow you to easily create enumerable classes: classes that allow you to iterate through their
collection of values via a ​foreach-in​ loop. Classes that implement the ​IEnumerable()​ interface allow you to use
foreach-in​ against them. The ​IEnumerable​ interface demands that you implement a ​GetEmumerator​ method
that returns an ​IEnumerator​ object. It's the ​IEnumerator​ object that allows a ​foreach-in​ to work the way it
does.

public class MyNewEnumerableClass


{
private String[] m_MyStrings;

public MyNewEnumerableClass()
{
this.m_MyStrings = new String[1000000];
for (int i = 0; i <= 999999; i++)
this.m_MyStrings[i] = "Hello!";
}
public System.Collections.IEnumerator GetEnumerator()
{
// yield does all of the work:
foreach (String str in this.m_MyStrings)
yield return str;
}
}

Anonymous Methods

C# supports delegates for invoking one or multiple methods. Delegates provide operators and methods for adding
and removing target methods, and are used extensively throughout the .NET Framework for events, callbacks,
asynchronous calls, and multithreading. However, you are sometimes forced to create a class or a method just for
the sake of using a delegate. In such cases, there is no need for multiple targets, and the code involved is often
relatively short and simple. Anonymous methods is a new feature in C# 2.0 that lets you define an anonymous
(that is, nameless) method called by a delegate.

For example, the following is a conventional SomeMethod method definition and delegate invocation:

class SomeClass
{
delegate void SomeDelegate();
public void InvokeMethod()
{
SomeDelegate del = new SomeDelegate(SomeMethod);
del();
}
void SomeMethod()
{
MessageBox.Show("Hello");
}
}
You can define and implement this with an anonymous method:
class SomeClass
{
delegate void SomeDelegate();
public void InvokeMethod()
{
SomeDelegate del = delegate()
{
MessageBox.Show("Hello");
};
del();
}
}

Partial Types

C# 1.1 requires you to put all the code for a class in a single file. C# 2.0 allows you to split the definition and
implementation of a class or a struct across multiple files. You can put one part of a class in one file and another
part of the class in a different file, noting the split by using the new partial keyword. For example, you can put the
following code in the file MyClass1.cs:

public partial class MyClass


{
public void Method1()
{...}
}
In the file MyClass2.cs, you can insert this code:
public partial class MyClass
{
public void Method2()
{...}
public int Number;
}

Das könnte Ihnen auch gefallen