• 委托系列整理



    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、原始委托demo
    //TestClass tc=new TestClass();
    // string name = "测试";
    // tc.TestMethod(name, ShowInfo);//这个方法执行时候,执行的内部委托部分,会执行到2部分
    #endregion

    #region 2、常规委托使用
    //Action ac = ShowInfo;//第一种采用方法直接赋予
    //ac();

    //第二种采用委托委托关键字做具体,这是一种简化,关键字
    //Action ac1 = delegate{

    // Response.Write("</br>这是测试net中系统已经定义过无返回值,无参数的Action委托,这是采用匿名方法的使用");
    //};
    //ac1();

    #endregion

    #region 3、匿名方法委托的使用
    //匿名方法的委托更是一种简化
    //Action at = () =>
    //{
    // Response.Write("</br>这是测试net中系统已经定义过无返回值,无参数的Action委托,这是采用匿名方法的使用");

    //};

    //at();
    #endregion

    #region 4、多播委托
    //Action att = ShowInfo;
    //att += ShowInfo1;
    //att();
    #endregion
    //自定义加delegate后缀,系统自带的使用Handler
    List<int> list = new List<int>() { 1,2,3,4,5,6,7,8,9,10};

    bool resultAll= list.All(x => x > 6);//要传入的委托Func<int,bool> 传入int 参数,返回值为bool的方法,list.ALL这个返回值和委托的返回值是否是一样的呢?
    bool resultAny= list.Any(x => x > 6);

    Response.Write("");

    }

    //2,委托使用时候,回来这里调用具体的方法
    public void ShowInfo(string name) {
    Response.Write(name);
    }

    //无返回值方法,
    public void ShowInfo(){
    Response.Write("这是测试net中系统已经定义过无返回值,无参数的Action委托");
    }
    public void ShowInfo1()
    {
    Response.Write("这是测试net中系统已经定义过无返回值,无参数的Action委托");
    }
    }
    }

    ----------------------------------------------------有些类同,不过底下有写内容比较详细

    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、正常委托的使用
    //string firstEvernt = User("test");

    /////do是关键字,不能用
    //deleOut str = new deleOut(User);

    //string delegateStr = str("k");//简写

    // str.Invoke("k");完整写法

    //Response.Write(delegateStr);
    #endregion
    #region 2、委托中使用匿名lambda,微软很多方法都是这么写的,可以起到伪装的作用,加了一层防护壳,也是多层安全性

    ///(x) => {
    /// return "你好!";这个是直接把方法写到委托中,另外写方法的

    ///}
    ///多参数就是(x,y)=>{
    /// x,y分别表示参数
    /// 只是采用匿名的方式传递
    ///
    /// }
    ///
    ///
    ///x=>x.user.equals(5) 是这个参数
    deleOut dele = new deleOut((x) =>
    {
    if (x == "什么")
    {
    return "你好!";
    }
    else
    {

    return "dfasdfdasf!";
    }


    });


    string user = dele("什么");

    Response.Write(user);
    #endregion
    string t = "";
    // Activator.CreateInstance(Type.GetType(t));
    //声明的委托与方法的参数一致,返回类型也要一致,委托就是copy了方法的功能换个名字而已。

    deleOutData dlod = new deleOutData(test.MathData);
    Response.Write(dlod(1));
    }

    #region 委托说明

    //1、普通委托
    //2、多播委托
    //3、lambda表达式真正在委托中的使用
    //4、Func、Action 表示的意思,Func<int,bool> 表示有返回值,int是传入参数类型,bool是返回值的类型
    // Action<int> 表示无返回值

    //5、 直接看List.FindAll() 方法,内部传递的也是委托,具体使用看,反编译

    #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>
    /// 委托,是数据类型,相当于一个类的级别,不要写在类里面,不要成为内部类的
    /// </summary>
    /// <param name="type"></param>
    public delegate string deleOut(string type);

    public delegate int deleOutData(int data);
    }

  • 相关阅读:
    视图的INSERT、UPDATE、DELETE注意事项
    SQL SERVER 用户管理 TSQL 命令
    SQL SERVER 利用存储过程查看角色和用户信息
    犯错了~
    配置tomcat
    python中的类继承之super
    python中参数解析
    python的几个内联函数:lambda ,zip,filter, map, reduce
    第一次性能测试http_load
    不能在 DropDownList 中选择多个项
  • 原文地址:https://www.cnblogs.com/linbin524/p/4767136.html
Copyright © 2020-2023  润新知