- 简单示例
/*
* 由SharpDevelop创建。
* 用户: David Huang
* 日期: 2015/7/27
* 时间: 10:22
*/
using System; namespace Delegate_Book
{
class Program
{
delegate string GetAString(); struct Currency
{
public uint Dollars;
public ushort Cents; public Currency(uint Dollars,ushort Cents)
{
this.Dollars=Dollars;
this.Cents=Cents;
} public override string ToString()
{
return string.Format("${0}.{1,2:00}",Dollars,Cents);
} public static string GetCurrencyUnit()
{
return "Dollar";
}
} public static void Main(string[] args)
{
const int x = ;
//GetAString getAString =new GetAString(x.ToString);
GetAString getAString=x.ToString;
Console.WriteLine(getAString()); Currency currency=new Currency(,);
getAString=currency.ToString;
Console.WriteLine(getAString()); Console.WriteLine(new GetAString(new Currency(,).ToString)()); getAString=Currency.GetCurrencyUnit;
Console.WriteLine(getAString()); Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
} - Action<T>和Func<T>委 托
/*
* 由SharpDevelop创建。
* 用户: David Huang
* 日期: 2015/7/28
* 时间: 14:31
*/
using System; namespace MathOperations
{
static class MathOperations
{
public static double MutiplyByTwo(double value)
{
return *value;
} public static double Square(double value)
{
return value*value;
}
} delegate double DoubleOp(double x); class Program
{ public static void Main(string[] args)
{
DoubleOp[] doubleOps={
MathOperations.MutiplyByTwo,
MathOperations.Square
}; foreach (var op in doubleOps) {
DoTheMath(op,2.5);
DoTheMath(op,3.5);
} Func<double,double>[] doubleFuncs={
MathOperations.MutiplyByTwo,
MathOperations.Square
}; foreach (var func in doubleFuncs) {
DoTheFunc(func,2.5);
DoTheFunc(func,3.5);
} Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
} static void DoTheMath(DoubleOp operation,double value)
{
double result=operation(value);
Console.WriteLine(string.Format("vlaue is {0},result is {1}",value,result));
} static void DoTheFunc(Func<double,double> func,double value)
{
var result = func(value);
Console.WriteLine(string.Format("value is {0},result is {1}",value,result));
} }
} - 冒泡排序示例
/*
* 由SharpDevelop创建。
* 用户: David Huang
* 日期: 2015/7/29
* 时间: 15:34
*/
using System;
using System.Linq;
using System.Collections.Generic; namespace BubbleSorter
{
class Program
{
public static void Main(string[] args)
{
//int
List<int> array=new List<int> {,,,,};
Sort(array);
Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(',')); //泛型
List<int> list=new List<int> {,,,,};
Sort(list,IsBiggerThan);
Console.WriteLine(array.ConvertAll(i=>i.ToString()).Aggregate((current,next)=>string.Format("{0},{1}",current,next)).TrimEnd(',')); //泛型
List<Person> personList=new List<Person>{
new Person{Name="Tom",Age=},
new Person{Name="Jack",Age=},
new Person{Name="Penny",Age=},
new Person{Name="Lucy",Age=}
};
Sort(personList,IsOlderThan);
foreach (Person person in personList) {
Console.WriteLine(person);
} Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
} static bool IsOlderThan(Person p1,Person p2)
{
return p1.Age>p2.Age;
} static bool IsBiggerThan(int a,int b)
{
return a>b;
} static void Sort(IList<int> sortArray)
{
bool swapped=true; do
{
swapped=false; for (int i = ; i < sortArray.Count-; i++) {
if (sortArray[i]>sortArray[i+]) {
int tmp=sortArray[i];
sortArray[i]=sortArray[i+];
sortArray[i+]=tmp;
swapped=true;
}
} }while (swapped);
} static void Sort<T>(IList<T> list,Func<T,T,bool> comparison)
{
bool swapped=true; do{
swapped=false; for (int i = ; i < list.Count-; i++) {
if (comparison(list[i],list[i+])) {
T temp=list[i];
list[i]=list[i+];
list[i+]=temp;
swapped=true;
}
} }while(swapped);
}
} class Person
{
public string Name{get;set;}
public int Age{get;set;} public override string ToString()
{
return string.Format("Name:{0},Age:{1}",Name,Age);
}
}
} - 多播委托
/*
* 由SharpDevelop创建。
* 用户: David Huang
* 日期: 2015/7/30
* 时间: 14:37
*/
using System; namespace 多播
{
public static class MathOperations
{
public static void MutiplyByTwo(double value)
{
Console.WriteLine(string.Format("method: MutiplyByTwo, value: {0}, result: {1}",value,*value));
} public static void Square(double value)
{
Console.WriteLine(string.Format("method: Square, value: {0}, result: {1}",value,value*value));
}
} class Program
{
public static void Main(string[] args)
{
Action<double> mathOperations=MathOperations.MutiplyByTwo;
mathOperations+=MathOperations.Square; DoTheMath(mathOperations,2.5);
DoTheMath(mathOperations,3.5); Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
} static void DoTheMath(Action<double> action,double value)
{
action(value);
}
}
} - 匿名委托
/*
* 由SharpDevelop创建。
* 用户: David Huang
* 日期: 2015/7/30
* 时间: 16:19
*/
using System; namespace 匿名
{
class Program
{
public static void Main(string[] args)
{
Func<int,int> anonDel=delegate(int i)
{
return i*;
}; Console.WriteLine(anonDel());
// TODO: Implement Functionality Here Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
相关文章
- C# 事件与委托
- 和lambda表达式
- C#中的委托和事件
- 事件与委托趣谈
- 事件委托:给多个元素和动态添加的元素绑定js事件,
- 委托、匿名函数、Lambda表达式和事件的学习
- C#中的委托是什么?事件是不是一种委托?委托与事件的区别?
- C#委托(delegate)的常用方式- 委托的定义 // 委托的核心是跟委托的函数结构一样 public delegate string SayHello(string c); public delegate string SayHello(string c);:定义了一个公共委托类型 SayHello,该委托接受一个 string 类型的参数 c,并返回一个 string 类型的值。 Main 方法 static void Main(string args) { // 本质上其实就是把方法当作委托的参数 SayHello sayC = new SayHello(SayChinese); Console.WriteLine(sayC("欢迎大家")); SayHello sayE = new SayHello(SayEgnlish); Console.WriteLine(sayE("Welcome to")); // 简单的写法:必须类型一样 SayHello s1 = SayChinese; SayHello s2 = SayEgnlish; Console.WriteLine(s1("好好好")); Console.WriteLine(s2("Gooood")); // 最推荐 SayHello ss1 = con => con; Console.WriteLine(ss1("niiiice")); // 匿名委托:一次性委托 SayHello ss3 = delegate(string s) { return s; }; Console.WriteLine(ss3("说中国话")); } 常规实例化委托 SayHello sayC = new SayHello(SayChinese);:创建了一个 SayHello 委托的实例 sayC,并将 SayChinese 方法作为参数传递给委托的构造函数。 Console.WriteLine(sayC("欢迎大家"));:通过委托实例调用 SayChinese 方法,并输出结果。 同理,SayHello sayE = new SayHello(SayEgnlish); 和 Console.WriteLine(sayE("Welcome to")); 是对 SayEgnlish 方法的委托调用。 简化的委托赋值方式 SayHello s1 = SayChinese; 和 SayHello s2 = SayEgnlish;:当委托类型和方法签名一致时,可以直接将方法赋值给委托变量,无需使用 new 关键字。 Console.WriteLine(s1("好好好")); 和 Console.WriteLine(s2("Gooood"));:通过委托实例调用相应的方法。 使用 Lambda 表达式实例化委托 SayHello ss1 = con => con;:使用 Lambda 表达式创建委托实例 ss1,con => con 表示接受一个参数 con 并返回该参数本身。 Console.WriteLine(ss1("niiiice"));:通过委托实例调用 Lambda 表达式。 匿名委托 SayHello ss3 = delegate(string s) { return s; };:使用匿名委托创建委托实例 ss3,delegate(string s) { return s; } 是一个匿名方法,直接在委托实例化时定义了方法体。 Console.WriteLine(ss3("说中国话"));:通过委托实例调用匿名方法。 委托引用的方法定义 public static string SayChinese(string content) { return content; } public static string SayEgnlish(string content) { return content; } public static string SayChinese(string content) 和 public static string SayEgnlish(string content):定义了两个静态方法,分别接受一个 string 类型的参数 content,并返回该参数本身。这两个方法的签名与 SayHello 委托一致,可以被 SayHello 委托引用。 常规的委托实例化、简化的赋值方式、Lambda 表达式和匿名委托。委托在 C# 中是一种强大的机制,它允许将方法作为参数传递,实现了代码的灵活性和可扩展性。
- lambda 委托 匿名方法
- jquery事件委托详解