People who are passionate about the C # language will like Visual C # 2005. Visual Studio 2005 brings a number of exciting new features to Visual C # 2005, such as generics, iterators, local classes, and anonymous methods. While generics are one of the most talked about and expected features, especially among C + + developers who are familiar with templates, other new features are also for Microsoft. NET development treasure the important supplement. Adding these features and languages to the first version of C # will increase your overall productivity, allowing you to write more concise code faster. For some background on generics, you should take a look at the sidebar, "What is generics?" ”。
Iterative Program
In C # 1.1, you can use a Foreach loop to traverse a data structure such as an array, a collection:
string[] cities = {"New York","Paris","London"};
foreach(string city in cities)
{
Console.WriteLine(city);
}
In fact, you can use any custom data set in a Foreach loop, as long as the collection type implements the GetEnumerator method that returns the IEnumerator interface. Typically, you need to do this by implementing the IEnumerable interface:
public interface IEnumerable
{
IEnumerator GetEnumerator();
}
public interface IEnumerator
{
object Current {get;}
bool MoveNext();
void Reset();
}
In general, classes that are used to traverse a collection by implementing IEnumerable are provided as nested classes of the collection type to traverse. This iterator type maintains the state of the iteration. It is often better to use a nested class as an enumerator because it can access all the private members of its containing class. Of course, this is an iterative programming pattern that hides the actual implementation details of the underlying data structure on the iterative client, allowing the same client-side iterative logic to be used on a variety of data structures, as shown in Figure 1.
Figure 1 Iterative Programming mode