zoukankan      html  css  js  c++  java
  • C#学习笔记:泛型委托Action<T>和Fun<TResult>

    转自:http://www.cnblogs.com/Joetao/articles/2094271.html

    本节学习了泛型委托Action<T>和Fun<TResult>两类特殊的委托,这两个特殊的委托是Dot FrameWrok自带的。结合lambda表达式,可以在写程序时,简洁代码和提高编码效率。

    (一)Action<T>和Fun<TResult>两个委托的不同点:

    Action<T>只能委托必须是无返回值的方法

    Fun<TResult>只是委托必须有返回值的方法

    (二)代码演练

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;


    //方法一:显式声明了一个委托,并将对 实例方法的引用分配给其委托实例。
    public delegatevoid ShowName();
    public delegatevoid ShowNameWithParameter(string name);
    public delegatevoid ShowAge(int age);
    public delegatevoid ShowNameAndAge(string name, int age);
    public delegateint ReturnName(string name);
    namespace ActionDemo
    {
    publicclass Person
    {
    privatestring instanceName;

    publicstring InstanceName
    {
    get { return instanceName; }
    set { instanceName = value; }
    }
    privateint instanceAge;

    publicint InstanceAge
    {
    get { return instanceAge; }
    set { instanceAge = value; }
    }
    public Person(string name,int age)
    {
    this.instanceName = name;
    this.instanceAge = age;
    }
    publicvoid DisplayName()
    {
    Console.WriteLine("Name:{0}",this.instanceName);
    }
    publicvoid DisplayName(string name)
    {
    Console.WriteLine("Name:{0}",name);
    }
    publicvoid DisplayAge(int age)
    {
    Console.WriteLine("Age:{0}",age);
    }
    publicvoid DisplayNameAndAge(string name, int age)
    {
    Console.WriteLine(string.Format("Name:{0} And Age:{1} ",name,age));
    }
    publicint GetAgeByName(string name)
    {
    if(name==instanceName)
    {
    return instanceAge;
    }
    else
    {
    return-1;
    }

    }

    }
    class Program
    {
    privatestaticvoid Print(string s)
    {
    Console.WriteLine(s);
    }


    staticvoid Main(string[] args)
    {
    #region Action<T>相关
    Person person =new Person("joetao",21);
    //非泛型委托
    //ShowName showName = new ShowName(name.DisplayName);
    //另一种写法
    ShowName showName = person.DisplayName;
    showName();
    ShowNameWithParameter showNameWithParameter = person.DisplayName;
    showNameWithParameter(person.InstanceName);
    ShowAge showAge = person.DisplayAge;
    showAge(person.InstanceAge);
    ShowNameAndAge showNameAndAge = person.DisplayNameAndAge;
    showNameAndAge(person.InstanceName, person.InstanceAge);

    string> actionShowName1 = person.DisplayName;
    actionShowName1(person.InstanceName);
    Action<int> actionShowAge = person.DisplayAge;
    actionShowAge(person.InstanceAge);
    Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
    actionShowNameAndAge(person.InstanceName, person.InstanceAge);

    Action<string> actionShowName1 = person.DisplayName;
    actionShowName1(person.InstanceName);
    Action<int> actionShowAge = person.DisplayAge;
    actionShowAge(person.InstanceAge);
    Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
    actionShowNameAndAge(person.InstanceName, person.InstanceAge);
    string> actionShowName1 = person.DisplayName;
    actionShowName1(person.InstanceName);
    Action<int> actionShowAge = person.DisplayAge;
    actionShowAge(person.InstanceAge);
    Action<string, int> actionShowNameAndAge = person.DisplayNameAndAge;
    actionShowNameAndAge(person.InstanceName, person.InstanceAge);
     
  • 相关阅读:
    防盗链(三)nginx实现图片防盗链(referer指令)
    防盗链(二)nginx secure_link下载防盗链
    防盗链(一)资料整理
    如何实现 token 加密
    Socket编程(三)demo举例
    Socket编程-(二)tcp三次握手与四次挥手
    Spring的事务控制,注解和xml配置以及解释
    SpringAop的核心概念和使用,表达式匹配刨析,xml配置通知和环绕通知
    SpringAop的简介
    Spring整合JUnit测试单元
  • 原文地址:https://www.cnblogs.com/BluceLee/p/5769998.html
Copyright © 2011-2022 走看看