当前位置:网站首页>C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
C graphical tutorial (Fourth Edition)_ Chapter 13 entrustment: what is entrustment? P238
2022-07-03 12:52:00 【superfreak】
using System;
delegate void MyDel(int value);// Declared a delegate , The return value is void, The accepted parameter is int
// namely : This delegate can represent such a function : no return value , And accept a parameter of type int Parameters of
namespace Chapter13Delegate_131WhatisaDelegate
{
class Program
{
void PrintLow(int value)// The return value is void The parameter is one int Function of type
{
Console.WriteLine("{0}-Low Value",value);
}
void PrintHigh(int value)// The return value is void The parameter is one int Another function of type
{
Console.WriteLine("{0}-High Value", value);
}
static void Main(string[] args)
{
Program program = new Program();
MyDel del;
// Random number generation 0-----99
Random rand = new Random();
int randomValue = rand.Next(99);
// Create a containing PrintLow or PrintHigh And assign it to del Variable
del = randomValue < 50 ? new MyDel(program.PrintLow) : new MyDel(program.PrintHigh);
// The above is to assign a delegate to a function , Or to whom this Commission is given ? Which function is given is determined , But it doesn't call
del(randomValue);// Perform entrusted
// The above statement is the real use of this delegate Add this Random values to run this delegate , It runs the function represented by the delegate .
Console.WriteLine("Hello World!");
}
}
}
边栏推荐
猜你喜欢
随机推荐
Comprehensive evaluation of double chain notes · Siyuan notes: advantages, disadvantages and evaluation
Kotlin notes - popular knowledge points asterisk (*)
4. Wireless in vivo nano network: electromagnetic propagation model and key points of sensor deployment
elastic_ L02_ install
Low code platform international multilingual (I18N) technical solution
Swift bit operation exercise
【计网】第三章 数据链路层(2)流量控制与可靠传输、停止等待协议、后退N帧协议(GBN)、选择重传协议(SR)
(最新版) Wifi分销多开版+安装框架
初入职场,如何快速脱颖而出?
最新版盲盒商城thinkphp+uniapp
Social community forum app ultra-high appearance UI interface
Sword finger offer06 Print linked list from end to end
Approve iPad, which wants to use your icloud account
01_ Using the concurrent tool class library, is thread safety safe
Airflow installation jump pit
【習題七】【數據庫原理】
ImportError: No module named examples. tutorials. mnist
社交社区论坛APP超高颜值UI界面
Applet wxss introduction
【ArcGIS自定义脚本工具】矢量文件生成扩大矩形面要素