Entrusted series finishing

Source: Internet
Author: User


Using Consoleappdelegate;
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Namespace Webdelegatefor_ref_out
{
public partial class DelegateDemo:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
#region 1, the original commissioned demo
TestClass tc=new TestClass ();
String name = "Test";
Tc. TestMethod (name, showinfo);//This method executes when the internal delegate portion of the execution is executed to 2 parts
#endregion

#region 2, General delegate use
Action AC = showinfo;//The first method of use directly gives
AC ();

The second is to use the delegate keyword to do the specific, this is a simplification, the keyword
Action AC1 = delegate{

Response.Write ("</br> This is a system in test net has defined no return value, no parameter of the action delegate, which is used by anonymous methods");
//};
AC1 ();

#endregion

#region 3. Use of anonymous method delegates
The delegation of anonymous methods is more a simplification
Action at = () =
//{
Response.Write ("</br> This is a system in test net has defined no return value, no parameter of the action delegate, which is used by anonymous methods");

//};

at ();
#endregion

#region 4, multicast delegation
Action att = showinfo;
Att + = ShowInfo1;
Att ();
#endregion
Custom add delegate suffix, the system comes with the use of handler
list<int> list = new list<int> () {1,2,3,4,5,6,7,8,9,10};

BOOL Resultall= list. All (x = = > 6);//The delegate to be passed in func<int,bool> the int parameter, the method that returns the value bool, list. Is all this return value the same as the return value of the delegate?
BOOL resultany= list. Any (x = x > 6);

Response.Write ("");

}

2, delegate use time, come back here to call the concrete method
public void Showinfo (string name) {
Response.Write (name);
}

No return value method,
public void Showinfo () {
Response.Write ("This is a system in test net has defined no return value, no parameter of the action Delegate");
}
public void ShowInfo1 ()
{
Response.Write ("This is a system in test net has defined no return value, no parameter of the action Delegate");
}
}
}

----------------------------------------------------somewhat similar, but there is a more detailed

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Namespace Webdelegatefor_ref_out
{
public partial class Delegate:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
#region 1, the use of normal delegation
String firstevernt = User ("test");

Do is a keyword and cannot be used
Deleout str = new Deleout (User);

string delegatestr = str ("k");//Shorthand

Str. Invoke ("K");

Response.Write (DELEGATESTR);
#endregion
#region 2, the delegation uses anonymous lambda, Microsoft many methods are so written, can play the role of camouflage, add a layer of protective shell, but also multi-layered security

(x) + = {
Return "Hello!" This is a direct way to write the method to the delegate, another way to write

///}
Multi-parameter is (x, y) =>{
x, y represent parameters respectively
It's just an anonymous way of passing
///
/// }
///
///
X=>x.user.equals (5) is this parameter
Deleout dele = new Deleout ((x) = =
{
if (x = = "What")
{
Return "Hello!" ";
}
Else
{

Return "dfasdfdasf! ";
}


});


String user = Dele ("what");

Response.Write (user);
#endregion
String t = "";
Activator.CreateInstance (Type.GetType (t));
The declared delegate is consistent with the parameters of the method, and the return type is identical, and the delegate is the function of the copy method to change the name.

Deleoutdata Dlod = new Deleoutdata (test. Mathdata);
Response.Write (Dlod (1));
}

#region Delegate Description

1. General Entrustment
2. Multicast delegation
3. The lambda expression is actually used in the delegate
4, Func, Action means that the meaning of,func<int,bool> represents a return value, int is the type of the passed parameter, bool is the type of the return value
Action<int> indicates no return value

5, directly see List.findall () method, internal transfer is also commissioned, concrete use to see, anti-compilation

#endregion


<summary>
///
</summary>
<param name= "QT" ></param>
<returns></returns>
public static string User (String qt) {

qt= "User";

return QT;
}


}
public class test{


public static int Mathdata (int data) {

return data * 2;
}

}

<summary>
The delegate, which is the data type, is equivalent to the level of a class, do not write in the class, do not become the inner class
</summary>
<param name= "Type" ></param>
Public delegate string Deleout (string type);

public delegate int Deleoutdata (int data);
}

Entrusted series finishing

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.