using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public delegate string ProcessDelegate(string s1, string s2);
class Program
{
static void Main(string[] args)
{
/* 调用方法 */
ProcessDelegate pd = new ProcessDelegate(new Test().Process);
Console.WriteLine(pd("Text1", "Text2"));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1,string s2)
{
return s1 + s2;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public delegate string ProcessDelegate<T,S>(T s1, S s2);
class Program
{
static void Main(string[] args)
{
/* 调用方法 */
ProcessDelegate<string,int> pd = new ProcessDelegate<string,int>(new Test().Process);
Console.WriteLine(pd("Text1", 100));
}
}
public class Test
{
/// <summary>
/// 方法
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public string Process(string s1,int s2)
{
return s1 + s2;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public delegate void ProcessDelegate(object sender, EventArgs e);
class Program
{
static void Main(string[] args)
{
/* 第一步执行 */
Test t = new Test();
/* 关联事件方法,相当于寻找到了委托人 */
t.ProcessEvent += new ProcessDelegate(t_ProcessEvent);
/* 进入Process方法 */
Console.WriteLine(t.Process());
Console.Read();
}
static void t_ProcessEvent(object sender, EventArgs e)
{
Test t = (Test)sender;
t.Text1 = "Hello";
t.Text2 = "World";
}
}
public class Test
{
private string s1;
public string Text1
{
get { return s1; }
set { s1 = value; }
}
private string s2;
public string Text2
{
get { return s2; }
set { s2 = value; }
}
public event ProcessDelegate ProcessEvent;
void ProcessAction(object sender, EventArgs e)
{
if (ProcessEvent == null)
ProcessEvent += new ProcessDelegate(t_ProcessEvent);
ProcessEvent(sender, e);
}
//如果没有自己指定关联方法,将会调用该方法抛出错误
void t_ProcessEvent(object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
void OnProcess()
{
ProcessAction(this, EventArgs.Empty);
}
public string Process()
{
OnProcess();
return s1 + s2;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace TestApp
{
/// <summary>
/// 委托
/// </summary>
/// <param name="s1"></param>
/// <param name="s2"></param>
/// <returns></returns>
public delegate string ProcessDelegate(string s1, string s2);
class Program
{
static void Main(string[] args)
{
/* 调用方法 */
Test t = new Test();
string r1 = t.Process("Text1", "Text2", new ProcessDelegate(t.Process1));
string r2 = t.Process("Text1", "Text2", new ProcessDelegate(t.Process2));
string r3 = t.Process("Text1", "Text2", new ProcessDelegate(t.Process3));
Console.WriteLine(r1);
Console.WriteLine(r2);
Console.WriteLine(r3);
}
}
public class Test
{
public string Process(string s1,string s2,ProcessDelegate process)
{
return process(s1, s2);
}
public string Process1(string s1, string s2)
{
return s1 + s2;
}
public string Process2(string s1, string s2)
{
return s1 + Environment.NewLine + s2;
}
public string Process3(string s1, string s2)
{
return s2 + s1;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有