func action c# code example

Example 1: c# func

using System;
using System.Collections.Generic;
using static System.Console;
namespace CSFlow.Delegates.Others
{
    public class ActionFunc
    {

        public static void Run ()
        {
            // Example of : Action
            // Use action when a mathod return void
            Action<string> display = new Action<string>(DisplayMessage);
            display("Calculate Discount :");

            // Example of : Func
            // Use action when a mathod return value
            Func<double, double> discount = new Func<double, double>(Discount);
            display(discount(12.5).ToString());

            List<Customer> custList = new List<Customer>();
            custList.Add(new Customer { Id = 1, FirstName = "Joydip", LastName = "Kanjilal", State = "Telengana", City = "Hyderabad", Address = "Begumpet", Country = "India" });
            custList.Add(new Customer { Id = 2, FirstName = "Steve", LastName = "Jones", State = "OA", City = "New York", Address = "Lake Avenue", Country = "US" });
            custList.Add(new Customer { Id = 3, FirstName = "Sefat", LastName = "Anam", State = "OSA", City = "New York", Address = "Manhatten", Country = "US" });

            // Example of : Predicate
            // Use Predicate for search data
            Predicate<Customer> FindAddress = customer => customer.Address == "Manhatten";

            Customer searchData = custList.Find(FindAddress);

            display($"{searchData?.FirstName} {searchData?.LastName} From - {searchData?.City} ");

            ReadKey();
        }

        static void DisplayMessage (string message)
        {
            WriteLine(message);
        }

        static double Discount (double money)
        {
            return money * .5;
        }

        class Customer
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string Address { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            public string Country { get; set; }
        }
    }
}

Example 2: c# func vs action

//A Action is a container to set a method for invoking that has no returns 
//where as a Func is a container to set a method for invoking that has returns
//so
//Any Generals or Templates in a Action will always be signiture input params
//Where as to say a function will alway have a output return indicated by the
//last General or Template type and any others will be inputs params.
//Addtionally async invokers (.BeginInvoke) with actions can be fire and forget 
//where as a Funcs must be awaited for the result using the EndInvoke
//example using lambda's (Short handed anonymous methods):
//Actions
Action BasicAction = ()=>{/*does something*/};
Action</*in*/ Type> SingleParamiterizedAction = (x)=>{/*does something*/};
Action</*in*/ Type1, /*in*/ Type2> DoubleParamiterizedAction = (x, y)=>{/*does something*/};
//....
Func</*out*/ TResult> BasicFunc = ()=>{/*does something*/ return TheTResult; };
Func</*in*/ Type,/*out*/ TypeResult> SingleParamiterizedFunc = (x)=>{/*does something*/ return TheTypeResult; };
Func</*in*/ Type1, /*in*/ Type2, /*out*/ TypeResult> DoubleParamiterizedFunc = (x, y)=>{/*does something*/ return TheTypeResult; }
//Usage example
BasicAction.Invoke(); //result of void can not be used
TResult TheResult = BasicFunc(); // result of the methods output can be used.