十二、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)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
随机推荐
- 正在执行的SQL和之前执行的SQL
SQL> select * from v$mystat where rownum<2; SID STATISTIC# VALUE ---------- ---------- ------- ...
- 【HDOJ】1930 And Now, a Remainder from Our Sponsor
简单字符串. #include <stdio.h> #define MAXLEN 160 char buf[MAXLEN]; ], parts[]; void getpart(int x) ...
- [FJSC2014]异或之
[题目描述] 给定n个非负整数A[1], A[2], ……, A[n]. 对于每对(i, j)满足1 <= i < j <= n,得到一个新的数A[i] xor A[j],这样共有n ...
- Android手机应用程序开发环境配置(Eclipse+Java+ADT)
参考: Java手机游戏开发实例简明教程 http://dev.10086.cn/blog/?uid-82940-action-viewspace-itemid-1772 Eclipse下载: htt ...
- HDOJ/HDU 2551 竹青遍野(打表~)
Problem Description "临流揽镜曳双魂 落红逐青裙 依稀往梦幻如真 泪湿千里云" 在MCA山上,除了住着众多武林豪侠之外,还生活着一个低调的世外高人,他本名逐青裙 ...
- 贵价的高级衣物清理法小Tipp之观察别人当然要从脚看起咯!
贵价的高级衣物清理法小Tipp之观察别人当然要从脚看起咯! 贵价的高级衣物清理法小Tipp之观察别人当然要从脚看起咯!
- 以O2O为核心的ERP思考
O2O已经火了一阵子了,很多人都在说O2O,各行各业都想和O2O有所结合,都认为这里面将会有巨大的商机. 在互联网发展到移动互联网的时代,我们的生活的很多方面已经被改变了,很多事情都已经可以在移动端完 ...
- 【matlab】matalb生成dll给Cpp用
http://blog.csdn.net/scudz/article/details/13628917 这篇文章写得很好,我跟据这个,操作了一下,发现,好用,精简版总结如下 1. matlab打开一个 ...
- 基于Lucene3.5.0怎样从TokenStream获得Token
通过学习Lucene3.5.0的doc文档,对不同release版本号 lucene版本号的API修改做分析.最后找到了有价值的修改信息. LUCENE-2302: Deprecated TermAt ...
- 一、Bitmap的recycle问题
尽管Android有自己的垃圾回收机制,对于是不是要我们自己调用recycle,还的看情况而定.假设仅仅是使用少量的几张图片,回收与否关系不大.但是若有大量bitmap须要垃圾回收处理,那必定垃圾回收 ...