C # 2.0: Use anonymous methods, iterators, and local classes to create elegant code

Source: Internet
Author: User
Tags foreach anonymous data structures visual studio

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.