Func delegate with no return type

前端 未结 7 1201
温柔的废话
温柔的废话 2020-12-02 03:54

All of the Func delegates return a value. What are the .NET delegates that can be used with methods that return void?

7条回答
  •  日久生厌
    2020-12-02 04:33

    A very easy way to invoke return and non return value subroutines. is using Func and Action respectively. (see also https://msdn.microsoft.com/en-us/library/018hxwa8(v=vs.110).aspx)

    Try this this example

    using System;
    
    public class Program
    {
        private Func FunctionPTR = null;  
        private Func FunctionPTR1 = null;  
        private Action ProcedurePTR = null; 
    
    
    
        private string Display(string message)  
        {  
            Console.WriteLine(message);  
            return null;  
        }  
    
        private string Display(string message1,string message2)  
        {  
            Console.WriteLine(message1);  
            Console.WriteLine(message2);  
            return null;  
        }  
    
        public void ObjectProcess(object param)
        {
            if (param == null)
            {
                throw new ArgumentNullException("Parameter is null or missing");
            }
            else 
            {
                Console.WriteLine("Object is valid");
            }
        }
    
    
        public void Main(string[] args)  
        {  
            FunctionPTR = Display;  
            FunctionPTR1= Display;  
            ProcedurePTR = ObjectProcess;
            FunctionPTR("Welcome to function pointer sample.");  
            FunctionPTR1("Welcome","This is function pointer sample");   
            ProcedurePTR(new object());
        }  
    }
    
        

    提交回复
    热议问题