C#委托实例
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Text;
namespace
weituo
{
class
Program
{
static
void
Main(
string
[] args)
{
// 声明委托变量
ProcessDelegate process;
Console.WriteLine(
"请输入用逗号分隔的两个数字:"
);
string
input = Console.ReadLine();
int
commaPos = input.IndexOf(
','
);
double
param1 = Convert.ToDouble(input.Substring(0, commaPos));
double
param2 = Convert.ToDouble(input.Substring(commaPos + 1,input.Length - commaPos -1));
Console.WriteLine(
"输入M乘法D除法"
);
input =Console.ReadLine();
// 初始化委托变量
if
(input ==
"M"
)
process =
new
ProcessDelegate(Multiply);
//注释:此处也可以写process = Multiply
else
process =
new
ProcessDelegate(Divide);
// 使用委托调用函数
double
result = process(param1,param2);
Console.WriteLine(
"结果:{0}"
,result);
Console.ReadKey();
}
// 声明委托
delegate
double
ProcessDelegate(
double
param1,
double
param2);
static
double
Multiply(
double
param1,
double
param2)
{
return
param1 * param2;
}
static
double
Divide(
double
param1,
double
param2)
{
return
param1 / param2;
}
}
}
C#委托实例的更多相关文章
- C#创建委托实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace MyDe ...
- C# 委托实例(跨窗体操作控件)
在C#里面却是可以不用自定义消息这么复杂的方法来实现跨窗体调用控件,C#有更好的办法就是委托. 效果描述:有两个窗体,FORM1(一个名为“打开form2”的button控件)和FORM2(一个名为“ ...
- 【C#】回调方法不通过object参数获得委托实例
回调方法中几乎都会存在获取委托实例的需求,进而通过委托实例调用EndInvoke以得到异步执行的返回值.在我看过的相关文章中,获取委托实例的方法几乎都是同一个,就是向BeginInvoke的最后一个参 ...
- C# 委托实例实现的多种类型
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Asp.Net 利用反射获得委托和事件以及创建委托实例和添加事件处理程序
子程序定义: public delegate void CurrentControlListenEvent(string uniqueID, string way = null); public ev ...
- jQuery的事件委托实例分析
事件委托主要是利用事件冒泡现象来实现的,对于事件委托的精准的掌握,可以有利于提高代码的执行效率.先看一段代码实例: <!DOCTYPE html> <html> <hea ...
- [C#]委托实例分析(附源码)
一直都听说C#中的委托与事件非常重要,都没有什么切身的体会,而这次通过做一个WinForm二次开发的项目才真正感觉到了委托与事件的犀利之处. 1.C#中的事件和委托的作用? 事件代表一个组件能够被关注 ...
- jQuery事件绑定和委托实例
本文实例讲述了jQuery事件绑定和委托.分享给大家供大家参考.具体方法如下: jQuery事件的绑定和委托可以用多种方法实现,on() . bind() . live() . delegate ...
- C#中委托和事件的区别实例解析
这篇文章主要介绍了C#中委托和事件的区别,并分别以实例形式展示了通过委托执行方法与通过事件执行方法,以及相关的执行流程与原理分析,需要的朋友可以参考下 本文实例分析了C#中委托和事件的区别,分享给大家 ...
随机推荐
- json 增删改 加 排序
<script type="text/javascript"> var json = { "age":24, "name":&q ...
- Android源码剖析之Framwork层消息传递(Wms到View)
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 前面讲过Wms.Ams与Activity的一系列交互,包括创建过程.消息传递.窗口展示等,紧接上篇介 ...
- HttpContext为null new HttpContextWrapper(System.Web.HttpContext.Current)
HttpContext = (context == null ? new HttpContextWrapper(System.Web.HttpContext.Current) : context);
- C++ 简易时间类
.h file #ifndef LIBFRAME_DATETIME_H_ #define LIBFRAME_DATETIME_H_ #include <stdint.h> #include ...
- Android Keycode详解
用JAVA写appium的testcase时,想用Android自带的物理返回键,网上找了下分享给大家. import io.appium.java_client.android.AndroidKey ...
- sql 主外键
alter table Orders add CONSTRAINT fk_PerOrders FOREIGN KEY(id) REFERENCES Persons(Id) 以上SQL中,Persons ...
- Interview Check If n Is A Perfect Square
Check if a given number is a perfect square with only addition or substraction operation. eg. 25 ret ...
- Flume-ng+Kafka+storm的学习笔记
Flume-ng Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统. Flume的文档可以看http://flume.apache.org/FlumeUserGuide.html ...
- Apache配置文件httpd.conf内容翻译
本文已经废弃,现在apache2不依靠httpd.conf来配置. Ubuntu下默认的配置文件是/etc/apache2/sites-available/default 可以修改上面文件来修改a ...
- python模块(sys)
SYS模块 sys.argv # 命令行参数List,第一个元素是程序本身路径 sys.exit(n) # 退出程序,正常退出时exit(0) sys.version # 获取Python解释程序的版 ...