日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

淺談C#中Action和Func回調的常用方式_C#教程

作者:熊思雨 ? 更新時間: 2022-05-24 編程語言

一、簡介

Action和Func泛型委托實際上就是一個.NET Framework預定義的委托,3.5引入的特性。基本涵蓋了所有常用的委托,所以一般不用用戶重新聲明。Action系列泛型委托,是沒有返回參數的委托,最多可以有16參數,也可以沒有參數。

Func系列的委托是有返回值的委托,最多可以有16個參數;元組是C# 4.0引入的一個新特性,編寫的時候需要基于.NET Framework 4.0或者更高版本。元組使用泛型來簡化一個類的定義.提供用于創造元組對象的靜態方法。最多可以提供創建新的 8 元組,即八元組。

二、Action

委托其實就是把方法當作參數來調用,Action就是其中之一,Action 作為參數不能有返回值,參數可以是任意類型,也可以不傳遞參數。

例1

調用某個類中的Action

using System;
 
namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            test1.myAction();
 
            Console.ReadKey();
        }
    }
 
    public class Test1
    {
        public Action myAction = null;
 
        private void sayHi()
        {
            Console.WriteLine("fuck you!");
        }
 
        public Test1()
        {
            myAction = sayHi;
        }
    }
}

運行:

這種方式用的比較少,Action常用的方式通常是用來作為和回調?

例2

執行一系列的操作后,再執行回調,也是比較推薦的使用方式。

using System;
 
namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            test1.Calculation(1, 2, ReceiveResult);
 
            Console.ReadKey();
        }
 
        private static void ReceiveResult(int res)
        {
            Console.WriteLine("結算的結果是:" + res);
        }
    }
 
    public class Test1
    {
        public void Calculation(int x, int y, Action call)
        {
            if (call != null)
            {
                call(x + y);
            }
        }
    }
}

運行:

將方法換成?Lambda 表達式,效果一樣的,關于Lambda的使用方法,可以參考:點擊跳轉

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            test1.Calculation(1, 2, (int res) =>
            {
                Console.WriteLine("結算的結果是:" + res);
            });
 
            Console.ReadKey();
        }
    }
 
    public class Test1
    {
        public void Calculation(int x, int y, Action call)
        {
            if (call != null)
            {
                call(x + y);
            }
        }
    }
}

三、Func

上面使用Action的案例中,執行回調后,都沒有返回值,這是因為Action并不能接收返回值,如果想執行回調,又有返回值怎么辦呢,Func就是用來解決這個問題的。

Func 必須有一個返回值,否則會報錯,如下圖:

返回值通常是在參數的最后一個,參考例1,Func MyFunc = null 這個委托中,string 就是返回值,傳遞參數的時候,也只能傳遞兩個參數,如果再多寫一個參數就會報錯,如下圖:

例1

基本的用法,func賦值,執行委托,并接收返回值

using System;
 
namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            string userName = test1.MyFunc(15, 180.2f);
            Console.WriteLine(userName);
 
            Console.ReadKey();
        }
    }
 
    public class Test1
    {
        public Func MyFunc = null;
 
        private string GetUserName(int age, float height)
        {
            if (age == 15 && height == 180.2f)
            {
                return "張三";
            }
            return null;
        }
 
        public Test1()
        {
            MyFunc = GetUserName;
        }
    }
}

運行:

例2

?把func作為方法的參數傳遞,并執行回調

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Test1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Test1 test1 = new Test1();
            Func func = () => 
            {
                string name = "張三";
                string feel = "精力非常旺盛";
                string msg = name + feel;
                return msg;
            };
            test1.Calculation(10, 12, func);
            Console.ReadKey();
        }
    }
 
    public class Test1
    {
        public void Calculation(int x,int y, Func sayFunc)
        {
            if(sayFunc != null)
            {
                int age = x + y;
                string msg = string.Format("年齡是:{0},對年齡的感受:{1}", age, sayFunc());
                Console.WriteLine(msg);
            }
        }
    }
}

運行:

上面代碼只是作為一個參考,讀者可以根據自己的需求做一個改進。

結束

原文鏈接:https://blog.csdn.net/qq_38693757/article/details/123041382

欄目分類
最近更新