十二、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)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
随机推荐
- semijoin链接进行subquery unnesting.
drop table emp1; drop table dept1; create table emp1 as select * from emp; create table dept1 as sel ...
- luoguP2265 路边的水沟
题目:http://www.luogu.org/problem/show?pid=2265 题解:ans=C(n+m,n)%p 求一下逆元就行 代码: #include<cstdio> # ...
- activiti集成drools实验
无代码,无真相. 网上的博客代码,都挺片段的.所以,我想找个现成的demo实验代码. 上github ------------------------------------------------- ...
- 初识DirectX和COM
一.COM 1.什么是COM对象 一个COM对象事实上是一个或一套实现了大量接口的C++类 2.COM的优点 不用重新编译你的程序就能升级COM模块 3.COM概貌 4.COM对象的接口 QueryI ...
- mac下的改装人生——mbp拆卸的各种资源整理
这几天弄了好多的mac上硬件的问题,我自己的mac也被我拆了3次,感觉自己终于跟硬件扯上关系了哈. 在这里放一些我找到比较好的资源教程,供大家学习,如果我以后还需要继续拆我的macbook,我也会回来 ...
- centos6.5建立cloudera-cdh4.6本地源
1.准备: centos6.5系统,root用户 2.安装所需包: sudo yum install yum-utils createrepo 3.下载cdh4.6的repo: ...
- 【转】Gabor 入门
Computer Vision Tutorials Search Primary Menu Skip to content Tutorials Search for: Gabor Filters ...
- 与IO相关的等待事件troubleshooting-系列5
'db file scattered read' 这是另一种常见的等待事件.他产生于Oracle从磁盘读取多个块到Buffer Cache中非连续(" scattered&q ...
- Java和JavaScript中使用Json方法大全
林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka 摘要:JSON(JavaScript Object Notation) 是一种轻量级的数 ...
- android生成验证码bitmap
不多说了,直接上代码,项目中用到的,未做优化,还有很多参数未设置. [java] view plaincopy 1.import java.util.Random; 2. 3.import andro ...