十二、C# 委托与Lambda表达式(匿名方法的另一种写法)
static class SimpleSort1
{
public static void BubbleSort(int[] items)
{
int i = , j = , temp = ;
if (items == null)
{
return;
}
for (i = items.Length - ; i >= ; i--)
{
for (j = ; j <= i; j++)
{
if (items[j - ] > items[i])
{
temp = items[j - ];
items[j - ] = items[i];
items[i] = temp;
}
} }
}
}
class Program
{
static void Main(string[] args)
{ int[] arr = new int[] { , , , , , , , , , , , };
SimpleSort1.BubbleSort(arr, SortType.Ascending);
string str = "";
for (int i = ; i < arr.Length; i++)
{
str += arr[i] + ","; }
Console.WriteLine(str); str = "";
SimpleSort1.BubbleSort(arr, SortType.Descending);
for (int i = ; i < arr.Length; i++)
{
str += arr[i] + ","; }
Console.WriteLine(str);
Console.ReadLine(); }
}
enum SortType
{
Ascending,
Descending
}
static class SimpleSort1
{
public static void BubbleSort(int[] items, SortType sorttype)
{
int i = , j = , temp = ;
if (items == null)
{
return;
}
for (i = items.Length - ; i >= ; i--)
{
for (j = ; j <= i; j++)
{
switch (sorttype)
{
case SortType.Ascending:
if (items[j - ] > items[i])
{
temp = items[j - ];
items[j - ] = items[i];
items[i] = temp;
}
break;
case SortType.Descending:
if (items[j - ] < items[i])
{
temp = items[j - ];
items[j - ] = items[i];
items[i] = temp;
}
break; } } } }
}
class DelegateSample
{
public delegate bool ComparisonHandler(int first, int second);
//相当于创建了一个数据类型:DelegateSample.ComparisonHandler
//因为它被定义成嵌套在DelegateSample中的一个类型。 }
虽然所有委托数据类型都间接从System.Delegate派生,但C#编译器并不允许定义一个直接或间接
从System.Delegate派生的类。 class Program
{
static void Main(string[] args)
{ int[] arr = new int[] { , , , , , , , , , , , };
string str = "";
//调用方法时,将指定的函数作为实际参数使用。使用方法来创建一个委托变量,委托是一个引用类型,但不必
//用new来实例化它。直接传递名称,而不是显式实例化,这是自C#2.0开始支持的一个新语法,称为委托推断 delegate interface
//采用这个语法,编译器将根据方法名来查找方法签名,并验证它同方法的参数类型匹配。
SimpleSort1.BubbleSort(arr, SimpleSort1.GreaterThan);
for (int i = ; i < arr.Length; i++)
{
str += arr[i] + ","; }
Console.WriteLine(str); str = "";
SimpleSort1.BubbleSort(arr, SimpleSort1.LonwerThan);
for (int i = ; i < arr.Length; i++)
{
str += arr[i] + ","; }
Console.WriteLine(str); str = "";
SimpleSort1.BubbleSort(arr, SimpleSort1.CharThan);
for (int i = ; i < arr.Length; i++)
{
str += arr[i] + ","; }
Console.WriteLine(str); Console.ReadLine(); }
} static class SimpleSort1
{
//使用委托数据类型 声明一个变量作为形式参数
public static void BubbleSort(int[] items, DelegateSample.ComparisonHandler compareMethod)
{
int i = , j = , temp = ;
if (items == null)
{
return;
}
for (i = items.Length - ; i >= ; i--)
{
for (j = ; j <= i; j++)
{
if (compareMethod(items[j - ], items[i]))
{
temp = items[j - ];
items[j - ] = items[i];
items[i] = temp;
}
}
}
} //以下四个函数都与数据类型DelegateSample.ComparisonHandler(委托) 具有同样的签名
public static bool GreaterThan(int first, int second)
{
return first > second;
}
public static bool LonwerThan(int first, int second)
{
return first < second;
}
public static bool CharThan(int first, int second)
{
int flag = (first.ToString()).CompareTo(second.ToString());
return (flag > ) ? true : false;
}
}
class DelegateSample
{
public delegate bool ComparisonHandler(int first, int second);
//相当于创建了一个数据类型:DelegateSample.ComparisonHandler }
DelegateSample.ComparisonHandler compareMethod;
compareMethod =
delegate(int first, int second)
{
return first > second;
};
SimpleSort1.BubbleSort(arr, compareMethod);
SimpleSort1.BubbleSort(arr,
delegate(int first, int second)
{
return first > second;
}
);
在任何情况下,匿名方法的参数和返回值类型都必须兼容于相对应的委托类型。
System.Func<int, int, bool> compareMethodFun;
compareMethodFun =
delegate(int first, int second)
{
return first > second;
};
Action<Object> broadAction = delegate(Object o)
{
Console.WriteLine(o);
};
Action<String> narrowAction = broadAction; Func<string> narrowFunction = delegate()
{
return Console.ReadLine();
}; Func<Object> broadFunction = narrowFunction;
Func<Object, String> narrowFunction = delegate(Object obj)
{ return obj.ToString();
}; Func<String, Object> broadFunction = narrowFunction;
SimpleSort1.BubbleSort(arr,
(int first, int second) =>
{
//可以有多个语句
return first > second;
}
);
SimpleSort1.BubbleSort(arr,
(first, second) =>
{
return first > second;
}
);
IEnumerable<Process> processes = Process.GetProcesses().Where(
process => { return process.WorkingSet64 > ( ^ ); });
SimpleSort1.BubbleSort(arr, (first, second) => first > second);
SimpleSort1.BubbleSort(arr, (int first, int second) => first > second);
System.Linq.Expressions.Expression<Func<int, int, bool>> expression;
expression = (x, y) => x > y;
十二、C# 委托与Lambda表达式(匿名方法的另一种写法)的更多相关文章
- Lambda表达式&匿名方法
“Lambda表达式“(lambda Expression)就是一个匿名函数(匿名方法),lambda表达式基于数学中的入演算得名. lambda运算符:所有的lambda表达式都是用新的lambda ...
- 委托,lambda,匿名方法
lambda表达式其实就是匿名方法的变体或者说简写. 原来我们用 delegate void Del(int x); Del d = delegate(int x) { return x + 1; } ...
- 深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types
匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
- 委托学习过程及委托、Lambda表达式和匿名方法的关系总结及事件总结
第一章,当开始学习委托的时候,我们会问什么是委托?为什么要学习委托? 一,什么是委托? 委托是一个类,它定义了方法的类型,使得可以将方法当作另一个方法的参数来进行传递,这种将方法动态地赋给参数的做法, ...
- 委托、匿名委托、Lambda 表达式、Expression表达式树之刨根问底
本篇不是对标题所述之概念的入门文章,重点在阐述它们的异同点和应用场景.各位看官,这里就不啰嗦了,直接上代码. 首先定义一个泛型委托类型,如下: public delegate T Function&l ...
- .net学习之新语法学习(匿名类和匿名方法,扩展方法,系统内置委托,Lambda表达式和linq等)
1.自动属性 Auto-Implemented Properties 2.隐式类型 var var变量不能作为全局变量使用,因为不能在编译时确定类型 3.参数默认值 和 命名参数 4.对象初始化器 ...
- 转载 C#匿名函数 委托和Lambda表达式
转载原出处: http://blog.csdn.net/honantic/article/details/46331875 匿名函数 匿名函数(Anonymous Function)是表示“内联”方法 ...
- 匿名函数、委托和Lambda表达式
匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
- [深入学习C#] 匿名函数、委托和Lambda表达式
匿名函数 匿名函数(Anonymous Function)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
随机推荐
- 51Testing招聘软件测试课程研发人员
最近有些两三年测试工作经验的小伙伴对自己的下一个工作有些迷茫,感觉很难有技术的突破,毕竟公司不是学校,不会允许员工海阔天空的去尝试各种新的技术.现在我就送给这些好学上进的小伙伴一个礼物,51Testi ...
- Windows作业
1.什么是Windows作业 Windows作业实际上一个进程组,可以给作业设置权限,一旦进程加入到作业内,进程的权限将会被作业限制. 2.创建一个作业 HANDLE CreateJobObject( ...
- linux 内核驱动加载过程中 向文件系统中的文件进行读写操作
utils.h 文件: #ifndef __UTILS_H__ #define __UTILS_H__ void a2f(const char *s, ...); #endif utils.c 文件: ...
- 12个强大的Web服务测试工具
在过去的几年中,web服务或API的普及和使用有所增加. web服务或API是程序或软件组件的集合,可以帮助应用程序进行交互或通过形成其他应用程序或服务器之间的连接执行一些进程/事务处理.基本上有两种 ...
- hdoj 2899 Strange fuction【二分求解方程】
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 银联手机支付(.Net Csharp),3DES加密解密,RSA加密解密,RSA私钥加密公钥解密,.Net RSA 3DES C#
前段时间做的银联支付,折腾了好久,拼凑的一些代码,有需要的朋友可以参考,本人.Net新手,不保证准确性! 这个银联手机支付没有SDK提供,技术支持也没有.Net的,真心不好搞! RSA加解密,这里有个 ...
- myeclipse svn
打开myeclipse的help---install from site 点击add弹出对话框 在输入框中输入对应内容 http://subclipse.tigris.org/update_1.10. ...
- tableview: 实现tableview 的 section header 跟随tableview滑动
方法一:(只有一个headerView)一段 如果你的tableview恰好只有一个headerView,实现这种效果就好办了.把要设置的headerView设置成tableView的header而不 ...
- UltraISO对光盘镜像的常用操作
UltraISO,它能直接编辑光盘映像或者从光盘映像文件里面提取文件:可以从CD-ROM里面制作光盘映像:也可以把硬盘上的文件制作成ISO文件:可以把ISO中启动信息保存下来,也可以为ISO添加启动功 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言)
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(30)-本地化(多语言) 我们的系统有时要扩展到其他国家,或者地区,需要更多的语言环境,微软提供了一些解决 ...