十二、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)是表示“内联”方法定义的表达式.匿名函数本身及其内部没有值或者类型,但是可以转换为兼容的委托或者表达式树类型(了解详情).匿名函数转换的计算取 ...
随机推荐
- 学习嵌入式Linux有没有一个最佳的顺序(持续更新)
作为一个嵌入式Linux的初学者,我知道我可能将长期处于初学者阶段,因为我至今仍然没有能够摸索出一条很好的道路让我由初学者进入到更高级阶段.但是我始终没有放弃,本篇文章就是用来记录我学习嵌入式Linu ...
- CXF Service Interceptor请求,响应报文之控制台输出
一:定义接口 @WebService(targetNamespace = "http://www.unionpay.com/client/appprovider", name = ...
- UIImageVIew的使用
UIImageView是一个用于显示图片的控件 构造方法: UIImage * tempImage = [UIImage imageNamed:IMAGE_NAME]; imageVi ...
- Hibernate的Session会话中get()和load()方法的区别
1.get和load都可以从数据库中获取数据 .get拿到的是真的对象,load拿到的是代理对象 2.get和load从数据库中获取数据,如果获取不到,get返回null,load会出现ObjectN ...
- android的生命周期
1.运行状态:当一个活动处于栈的顶部时,这时活动就处于活动状态,系统是不愿意回收处于活动状态的,会影响用户体验. 2.暂停状态:当一个活动不再处于栈的顶部时,但仍然可见时,这时就是暂停状态了.处于暂停 ...
- c#基础语言编程-程序集和反射
程序集 什么是程序集? 1.程序集(assembly)是一个及一个以上托管模块,以及一些资源文件的逻辑组合. 2.程序集是组件复用,以及实施安全策略和版本策略的最小单位. 3.程序集是包含一个或者多个 ...
- hive 配置文件以及join中null值的处理
一.Hive的參数设置 1. 三种设定方式:配置文件 · 用户自己定义配置文件:$HIVE_CONF_DIR/hive-site.xml · 默认配置文件:$HIVE_CONF_DIR/hi ...
- Eclipse启动 报错[Failed to load the JNI shared library jvm.dll
准备要做java服务器,在安装开发环境时,启动Eclipse报错[Failed to load the JNI shared library jvm.dll] 研究了下,造成错误的原因是由于eclip ...
- Swift语言入门之旅
Swift语言入门之旅 学习一门新的计算机语言,传统来说都是从编写一个在屏幕上打印"Hello world"的程序開始的.那在 Swift,我们使用一句话来实现它: printl ...
- 关于Sublime Text3 pyV8无法加载的问题
昨天切换到sublime text 3 安装 emmet插件 不起作用 提示 pyv8 无法加载 手动下载安装解决 问题描述 PyV8 Binaries Archive of pre-compi ...