首页 > 其他 > 详细

委托的4种写法

时间:2015-07-04 11:03:15      阅读:235      评论:0      收藏:0      [点我收藏+]
委托1:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplicartion2
{
    public delegate void G();//定义一个委托
    public partial class Form1 : Form
    {

        public static void Main(string[] args)//调用方法
        {
            G g = new G(W);
            g();
            Console.ReadKey();
        }
        private static void W()//写了一个方法
        {
            Console.Write("你好");
        }
    }
}

带参数的委托
namespace WindowsFormsApplicartion2
{
    public delegate void G(string a,string b);
    public partial class Form1 : Form
    {

        public static void Main(string[] args)
        {
            G g = new G(W);
            g("你好","再见");
            Console.ReadKey();
        }
        private static void W(string a,string b)//方法
        {
            Console.Write(a+b);
        }
    }
}



另一个写法
namespace WindowsFormsApplicartion2
{
    public delegate void G(string a,string b);
    public partial class Form1 : Form
    {

        public static void Main(string[] args)
        {
            G g = new G(delegate {
                Console.WriteLine("你好");
            
            });
           
        }

另一方法的带参数的委托

namespace WindowsFormsApplicartion2
{
    public delegate void G(string a,string b);
    public partial class Form1 : Form
    {

        public static void Main(string[] args)
        {
            G g = new G(delegate(string a, string b) { Console.WriteLine(a + b); });
            g("你好","再见");
            Console.ReadLine();
            //g("你好","再见");
            //Console.ReadKey();
        }

 

委托的4种写法

原文:http://www.cnblogs.com/w-wz/p/4620215.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!