delegate-1

using System;
using System.Collections;
using System.Collections.Generic;

namespace Delegate
{
    class Program
    {
        //1.定义委托类型
        public delegate int SumDelegate(IEnumerable src);

        public static int Meadia(SumDelegate sum)
        {
            int[] src = { 1, 3, 5 };
            //4.委托调用
            return sum(src);
        }

        static void Main(string[] args)
        {
            //2.创建委托实例
           SumDelegate sumDelegate =  new SumDelegate(Demo.Sum);
            //3.将委托作为参数传递
            int s = Meadia(sumDelegate);
            Console.WriteLine($"S = {s}");
          
        }

        
    }
    class Demo

    {
        public static int Sum(IEnumerable src)
        {
            int s = 0;
            foreach (int i in src)
            {
                s += i;
            }
            return s;

        }

        public static double Avg(IEnumerable src)
        {
            double s = 0, count = 0;
            foreach (int i in src)
            {
                s += i;
                count++;
            }
            return s/count;

        }


    }
}

你可能感兴趣的:(C#)