1.
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DelegateDemo
{
public class DelegateClass
{
//public static int Add(int x,int y)
//{
// return x + y;
//}
//public static int Multiply(int x,int y)
//{
// return x * y;
//}
public static void Add( int x, int y)
{
int addResult = x + y;
MessageBox.Show(addResult+"");
}
public static void Multiply( int x, int y)
{
int mulResult = x * y;
MessageBox.Show(mulResult+"");
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace DelegateDemo
{
public class DelegateClass
{
//public static int Add(int x,int y)
//{
// return x + y;
//}
//public static int Multiply(int x,int y)
//{
// return x * y;
//}
public static void Add( int x, int y)
{
int addResult = x + y;
MessageBox.Show(addResult+"");
}
public static void Multiply( int x, int y)
{
int mulResult = x * y;
MessageBox.Show(mulResult+"");
}
}
}
2.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//public delegate int MyDelegate(int x,int y);
public delegate void MulDelegate( int x, int y);
private void bt_First_Click( object sender, EventArgs e)
{
//MyDelegate dele = new MyDelegate(DelegateClass.Add);
//int addResult = dele(5, 5);
//MessageBox.Show(addResult+"");
//MyDelegate dele2 = new MyDelegate(DelegateClass.Multiply);
//int multiplyResult = dele2(2 , 3);
//MessageBox.Show(multiplyResult+"");
MulDelegate mul = new MulDelegate(DelegateClass.Add);
mul += new MulDelegate(DelegateClass.Multiply);
mul(4, 7);
mul -= new MulDelegate(DelegateClass.Add);
mul(7, 7);
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DelegateDemo
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
//public delegate int MyDelegate(int x,int y);
public delegate void MulDelegate( int x, int y);
private void bt_First_Click( object sender, EventArgs e)
{
//MyDelegate dele = new MyDelegate(DelegateClass.Add);
//int addResult = dele(5, 5);
//MessageBox.Show(addResult+"");
//MyDelegate dele2 = new MyDelegate(DelegateClass.Multiply);
//int multiplyResult = dele2(2 , 3);
//MessageBox.Show(multiplyResult+"");
MulDelegate mul = new MulDelegate(DelegateClass.Add);
mul += new MulDelegate(DelegateClass.Multiply);
mul(4, 7);
mul -= new MulDelegate(DelegateClass.Add);
mul(7, 7);
}
}
}
转载于:https://blog.51cto.com/yardan/67349