C# 学习Action与Func看Virtual。

通过代码简要描述Action、Func和Virtual。同时还可以了解到匿名委托和Lambda表达式的使用方法。
一、简要描述Action、Func和Virtual
1.Action与Func都是委托的一种,区别在于一个没有返回参数,一个有返回参数。
2.Virtual: 关键字用于修饰方法、属性、索引器或事件声明,并且允许在派生类中重写这些对象。例如,此方法可被任何继承它的类重写。
★★★需要注意的是: virtual  修饰符不能与  static abstract, private  或  override  修饰符一起使用。
3. 在MSDN中有详细的解释这里就不做过多的解释。下面我们就来看看代码!

二、 Action :表示无参,无返回值的委托
   Action<int,string> :表示有传入参数int,string无返回值的委托
   Action<int,string,bool> :表示有传入参数int,string,bool无返回值的委托
       Action<int,int,int,int> :表示有传入4个int型参数,无返回值的委托
   Action至少0个参数,至多16个参数,无返回值。
下面代码演示的是 Action 无参例子:
1. ParentClass
    class ParentClass
    {
        public virtual void ParentFunction(string mString, Action mOnShow)
        {
            Console.WriteLine("-----------ParentActionStart-------");
            //?.Invoke是简写<简化委托调用>,等价于mOnShow != null 就调用mOnShow();
            mOnShow?.Invoke();
            Console.WriteLine("-----------ParentActionEnd-------");
        }
    }
2.ChildrenClass
   class ChildrenClass : ParentClass
    {
        public override void ParentFunction(string mString, Action mOnShow)
        {
            base.ParentFunction(mString, mOnShow);
            Console.WriteLine("-----------ChildrenActionStart-----------");
            //判断字符串是否为空
            if (!string.IsNullOrEmpty(mString))
            {
                //?.Invoke是简写,等价于mOnShow != null 就调用mOnShow();
                mOnShow?.Invoke();
                if (mOnShow != null)       //等价于上面的步骤
                {
                    mOnShow();
                }
                //分开用两个if是为了介绍简化委托调用
                if (mOnShow == null)
                {
                    //使用Lambda表达式进行赋值
                    mOnShow = ()=>{ Console.WriteLine("-----------Children Assginment"); };
                }
                Console.WriteLine("-----------ChildrenActionEnd-----------");
            }
        }
    }
3.main
    class Program
    {
        static void Main(string[] args)
        {
            ChildrenClass _children = new ChildrenClass();
            string _str = "-----------Main-------------";
            //使用匿名委托进行赋值
            _children.ParentFunction(_str, delegate { Console.WriteLine("-----------MainAction-----------"); });
        }
    }
4.运行结果

三、 Func :是有返回值的泛型委托
   Func<string> :表示无参,返回值为string的委托
   Func<int,bool> 表示传入参数为int返回值为bool的委托
   Func<T1,T2,,T3,int> 表示传入参数为T1,T2,,T3(泛型)返回值为int的委托
   Func<object,string,int> 表示传入参数为object, string 返回值为int的委托
   Func至少0个参数,至多16个参数,根据返回值泛型返回。必须有返回值,不可void

下面的代码分别演示Func<string>和Func<int,bool>
首先是:《Func<string> 》表示无参,返回值为string类型的委托。
1.parentClass
    class ParentClass
    {
        public virtual void ParentFunction(string mString , Func<string> mOnShow)
        {
            Console.WriteLine("-----------ParentFuncStart-------");
            //?.Invoke是简写<简化委托调用>,等价于mOnShow != null 就调用mOnShow();
            string _str = mOnShow?.Invoke();
            Console.WriteLine("-----------Parent mOnShow:" + _str);
            Console.WriteLine("-----------ParentFuncEnd-------");
        }
    }
2.ChildrenClass
   class ChildrenClass : ParentClass
    {
        public override void ParentFunction(string mString, Func<string> mOnShow)
        {

            base.ParentFunction(mString, mOnShow);
            Console.WriteLine("-----------ChildrenFuncStart-----------");
            //判断字符串是否为空
            if (!string.IsNullOrEmpty(mString))
            {
                //?.Invoke是简写,等价于mOnShow != null 就调用mOnShow();
                string _str = mOnShow?.Invoke();
                if(mOnShow != null)       //等价于上面的步骤
                {
                    _str = mOnShow();
                }
                Console.WriteLine("-----------Children mOnShow:" + _str);
                //分开用两个if是为了介绍简化委托调用
                if (mOnShow == null)
                {
                    //使用Lambda表达式进行赋值
                    mOnShow = ()=>{ return "-----------ReadChildrenFunc-----------"; };
                    string _mStr = mOnShow();
                    Console.WriteLine("-----------ReadChildrenFunc mOnShow:" + _str);
                }
                Console.WriteLine("-----------ChildrenFuncEnd-----------");
            }
        }
    }
3.Main
    class Program
    {
        static void Main(string[] args)
        {
            ChildrenClass _children = new ChildrenClass();
            string _str = "--------Main-------------";
            //使用匿名委托进行赋值
            _children.ParentFunction(_str, delegate { return "MianFunc"; });
        }
    }
4.运行结果

其次是:《Func<int,bool>》表示传入参数为int类型,返回值为bool类型的委托
1.ParentClass
    class ParentClass
    {
        public virtual void ParentFunction(int mSum , Func<int,bool> mOnShow)
        {
            Console.WriteLine("-----------ParentFuncStart-------");
            if(mOnShow != null)
                Console.WriteLine("-----------ParentFuncBool-------");
            Console.WriteLine("-----------ParentFuncEnd-------");
        }
    }
2.ChildrenClass
    class ChildrenClass : ParentClass
    {
        public override void ParentFunction(int mSum, Func<int,bool> mOnShow)
        {
            base.ParentFunction(mSum, mOnShow);
            Console.WriteLine("-----------ChildrenActionStart-----------");
            //可以使用?.Invoke(mSum)简写,,但要注意:如果mOnShow为空的话就会运行时出错。
            //if ((bool)mOnShow?.Invoke(mSum))
            //{
                //Console.WriteLine("-----------ChildrenFuncBool-----------");
            //}
            if (mOnShow != null)  
            {
                mOnShow(mSum);
                Console.WriteLine("-----------ChildrenFuncBool-----------");
            }
            //分开用两个if是为了介绍简化委托调用
            if (mOnShow == null)
            {
                //使用Lambda表达式进行赋值
                mOnShow = (sum) =>{return mSum == 10; };
                if (!mOnShow(mSum))
                {
                    Console.WriteLine("-----------ChildrenFuncBool---mSum != 10--------");
                }
            }
            Console.WriteLine("-----------ChildrenActionEnd-----------");
        }
    }
3.main
    class Program
    {
        static void Main(string[] args)
        {
            ChildrenClass _children = new ChildrenClass();
            string _str = "-----------Main-------------";
            int _sum = 5;
            //使用匿名委托进行赋值
            _children.ParentFunction(_sum, null );
        }
    }
4.运行结果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值