📜  C# func - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:38.830000             🧑  作者: Mango

代码示例6
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 display = new Action(DisplayMessage);
            display("Calculate Discount :");

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

            List custList = new List();
            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 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; }
        }
    }
}