Csharp 高级编程 C7.1.2

第七章 代理(1)

一、代理要声明

二、代理使用步骤

  • 声明代理
  • 初始化代理(使用 实例的方法名 作为参数)
  • 使用代理

代码示例:

/*C7.1.2*/

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;



namespace C7._1._2

{

    class Program

    {

        //声明代理

        private delegate string getStr();



        static void Main(string[] args)

        {

            int x = 10;

            getStr testDeleMethod = new getStr(x.ToString );  //初始化的参数是   实例的一个方法名

            Console.WriteLine("Delegate Method Test: the string type of x is " + testDeleMethod());

            Console.ReadLine();

        }

    }



}

 

你可能感兴趣的:(sha)