Asp.net 2.0 tutorial C #2.0 anonymous method, iterator, local class

Source: Internet
Author: User

Respect the author. Keep the words www.it55.com.

In this section, we will continue to learn about the new features of C #2.0: anonymous methods, iterators, and local classes.
These new features can be said to be one of the essential parts of C #2.0. Using these new features can greatly reduce the code repetition rate and reduce the amount of code writing.
Who threw the eggs? Didn't I just say a few nonsense words? Does this apply to me? Okay. Let's get started together!
1. Anonymous Method
We have discussed the use of delegation in the previous section, and you will feel very troublesome. First, create a static class or method, then declare a delegate and construct it.
The arrival of the anonymous method made everyone very excited. Let's take a look at a simple example:

Button Bt;
Public void test ()
{
Bt. Click + = delegate {MessageBox. Show ("You clicked the button! ");};
}
This is required if delegated implementation is used:

Button Bt;
Public void test ()
{
Bt. Click + = new eventhandler (addclick); // eventhandler is actually a delegate in the system class library.
}
.....
Void addclick (Object sender, eventargs E)
{
MessageBox. Show ("You clicked the button! ");
}

The above is an anonymous method without parameters. If you use an anonymous method with parameters, you can achieve this (the number and type of parameters must be consistent ):

Bt. Click + = delegate (Object OBJ, eventargs ER) {MessageBox. Show ("You clicked the button! ");};

The anonymous method allows the code associated with a delegate to be embedded in the place where the delegate is used as a code block. The constructor process of the delegate method can be omitted.
In addition, the anonymous method can share access to local function members:

Class Test
{
Delegate void D ();
Static void main ()
{
Bool flag = false;
D = delegate {
If (FLAG) // read local member flag
Console. writeline ("true ");
Else
Flag = true; // rewrite the local member flag
};
}
}

2. iterator
In order to explain the iterator in C #2.0, we need to first understand the interface classes in the following framework:

(1) aggregate system. Collections. ienumerable
(Create system. Collections. ienumerator as a subclass)

Public System. Collections. ienumerator getenumerator ()
Ienumerable can be used to generate an ordered aggregation object set.

(2) Enumeration type. Purpose: Enumerate aggregation object sets.
System. Collections. ienumerator
Method:
Public object current
Public void reset ()
Public bool movenext ()

Let's talk about the iterator in C #2.0. With the iterator, we can easily enumerate classes and struct without implementing the ienumerable interface or members of the ienumerator interface.
Let's look at an example:

// Creation Method
Public class test: system. Collections. ienumerable
{
String [] V = {"A", "B", "C", "D", "E", "F", "G"}; // Of Course, it can also be a list of objects of a class.
Public static system. Collections. ienumerator getenumerator ()
{
For (INT I = 0; I <v. length; I ++)
{
Yield return V [I];
}
}
}
// Method 1
While (test. getenumerator (). movenext ())
{
String OBJ = (string) EE. Current;
System. Console. writeline (OBJ );
}
// Method 2: Use it implicitly
Foreach (string obj1 in test. getenumerator ())
{
System. Console. writeline (obj1 );
}
The output results of the above program (the results of the two application methods are the same ):
A
B
C
D
E
F
G

3. Local class
In C #1. X, we only allow the content of one class to exist in only one file, but if the code is too long, it will cause inconvenience to our reading and maintenance work.
Local classes allow the contents of classes, structures, and interfaces to be divided into small pieces and stored in multiple files. This not only avoids the above problems, in addition, we can add new functions or improve the method attributes for the class without damaging the original integrity of the class. # P # paging title # e #
The partial class must use the partial keyword to create a class.

Public partial class test: idisposable
{
Public String getstr ()
{
Return "partialtest ";
}
}

// The partial class can be written in different files. In fact, it is the Union set, and the class modifiers must be consistent.
Public partial class test
{
Public int getint ()
{
Return 1;
}
}

Public partial class partialtest: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Test Pt = new test ();
Response. Write (Pt. getstr ());
Response. Write (Pt. getint (). tostring ());
}
}

Restrictions on local types:
The so-called local method is limited to the class itself and does not support internal methods of the class.
The local type is only applicable to classes, structures, or interfaces, and does not support delegation or enumeration.
Each part of the same type must have a modifier partial.
When using a local type, each part of a type must be in the same namespace.
Each part of a type must be compiled at the same time.

So far, we have finished learning the new features of C #2.0. In the next section, we will go to ASP. NET 2.0 again and continue to learn the new features of ASP. NET 2.0: master page.

Related Article

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.