Network Programming Experience Tips: asp.net2.0 generics and Anonymous methods

Source: Internet
Author: User
Tags anonymous
Asp.net| Programming | skills | network

generic type:

Generics Overview: Generics can be applied to classes, methods, structures, interfaces, delegates, and other designs, concurrent reuse, type safety and efficiency in one is with the right on the non-generic type of application generics, you must use the class System.Collections.Generic.Stack. The class is declared in the following way:

System.Collections.Generic.Stack (Declaration code)

You can think of the stack class as having the following schematic processing process:

Stack class schematic code:

public class Stack<>
{
T[] Item;
int count;
public void Push (T item) {}
Public T Pop () {}
}

You can set constraints on 3 types of parameters for generics: Derivation constraints, constructor constraints, reference/value type constraints

anonymous method:

Overview of anonymous methods: In c#1.x, events are implemented using delegates that support calling methods. Delegates provide operators and methods to add or delete a target method, or to the entire. NET Framework is widely used in events, callbacks, asynchronous invocations, multithreading, and so on. However, in order to use a delegate, sometimes having to create a class or method is too cumbersome and inconsistent with the developer's thinking habits. For example, the following example enables you to get simple input from a form that contains list boxes, text boxes, and buttons. When you press the button, the text in the text box is added to the list box. In c#1.x, you use a delegate to implement the event mechanism, as shown in the following code

Using the c#1.x implementation code

Class Inputform:from
{
ListBox listbox;
TextBox textbox;
Button AddButton;
Pulic MyForm ()
{
ListBox = new ListBox ();
TextBox = new TextBox ();
AddButton = new Button ();
}
........
void AddClick (Object Sender,eventargs e)
{
LISTBOX.ITEMS.ADD (TextBox.Text)
}
}

Anonymous methods allow the associated code (usually the method entity) to be "embedded" into the place where the delegate is used, so that both the delegate and the method entity are lumped together

Implementing code with anonymous methods (no parameters)

Class Inputform:from
{
ListBox listbox;
TextBox textbox;
Button AddButton;
Pulic MyForm ()
{
ListBox = new ListBox ();
TextBox = new TextBox ();
AddButton = new Button ();
Addbutton.click + = delegate {listBox.Items.Add (textbox.text);}
}
}



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.