当前位置:网站首页>【小5聊】C#基础之内置和自定义委托的简单使用
【小5聊】C#基础之内置和自定义委托的简单使用
2022-07-15 18:38:00 【小5聊】
最近一段时间都在面试,最大的感概就是,没有掌握太多核心的技术,所以随着年龄的增长,其实优势就没有了,自然得不到技术官或者总监的认可。
不管如何,学习肯定是不能停的,由于个人理解方面会比较差,所以需要后天很多实战进行积累来辅助我的理解,在这个阶段已经开始慢慢多很多知识点的回顾加深了一定的理解,从而能够对框架和编程更加有帮助
1、什么是委托
这个在面试的时候,被问到的概率也会很高
什么是委托呢? 简单的理解就是,委托是执行一个方法或者某个方法体,和前端js的callback类似
1)委托是一个类
关键词:delegate
有点类似定义了一个interface接口方式一样,先定义不实现

2)委托属于一个引用类型
自然就拥有引用类型的特点,不会保存实际值,而是保存存储在托管堆中的对象的引用,保存函数的引用
3)委托定义了方法的类型
能够将方法当作参数在另一个方法里传递,可以避免在方法里使用大量if-else或者switch
4)委托最大特点是能够最大化的扩展
5)委托类型安全属性
.NET中的委托是类型安全的,委托会检测它所保存的函数引用是否和声明的委托匹配。
2、内置委托Action
假设有如下txt文本内容,需要读取出来,然后通过分组将题目和答案分开

代码如下
public void Temp()
{
try
{
string path = @"C:\86_svn\question_collect\files";
string content = Get(path, "data");
string[] list = content.Split("\r\n");
foreach (string item in list)
{
if (string.IsNullOrEmpty(item)) continue;
string item_value = item.Replace("(", "(");
string question_content = "";
string answer_value = "";
string[] arr = item_value.Split('(');
// 定义内置委托
Action action1 = new Action(() =>
{
question_content = arr[0];
if (question_content.Split('.').Length > 0)
{
question_content = question_content.Split('.')[1];
}
for (int i = 1; i < arr.Length; i++)
{
answer_value += arr[i];
}
answer_value = answer_value.TrimEnd(')').TrimEnd(')');
});
if (arr.Length > 1)
{
action1();
}
else
{
arr = item_value.Split('(');
if (arr.Length > 1)
{
action1();
}
}
}
}
catch (Exception exception)
{
}
finally
{
}
}
#endregion
public static string Get(string path_value, string file)
{
string str = "";
try
{
string pathFile = path_value;
string fileName = "/" + file + ".txt";
using (StreamReader r = new StreamReader(pathFile + fileName))
{
str = r.ReadToEnd();
}
}
catch (Exception ex)
{
LmyLogHelper.Error(ex);
}
return str;
}3、自定义委托

public delegate int MyDelegate(int a);
public void Temp()
{
MyDelegate tt = new MyDelegate((int a) =>
{
int b = a + 5;
return b;
});
int c = tt(6);
}边栏推荐
- Basic learning about MySQL
- MySQL安装时一直卡在starting the server的解决方案
- Common differences between MySQL and Oracle (2)
- 【Luogu_P4556】 [Vani有约会]雨天的尾巴 /【模板】线段树合并
- 事件4624是登录成功!?!真的如此吗?
- How to solve the crash when the easygbs platform edits the device management group?
- 但斌投资峰会:论资产管理的重要性!
- 【集训DAY1】Maximum benefit【离散化】【贪心】
- 使用EventLog Analyzer进行实时用户会话跟踪
- In the throes of household appliance market transformation, SaaS system platform is applied to inject new momentum into the development of household appliance industry
猜你喜欢

关于2022年3月APT-C-41伪装为WinRar.exe攻击的终端侧应急响应排查点

记录记录记录

Measurement of conductive potential of thin film copper foil

国产版Postman工具,好用!

Top level planning scheme of "smart forest and grass" in Heilongjiang

【集训DAY3】 Section【贪心】【二分】

Set round avatar -- canvas and paint

上海的这一新数字规划,为 NFT、元宇宙等带来了新的可能性

OpenCV 教程 01:简介与安装,图片与视频的基本操作

(codeforce319)B.Psychos in a Line(单调栈)
随机推荐
(codeforce1699)A&B (构造)
Lambda表达式二:方法引用
[leetcode weekly -- hash table simulation] 6113 The smallest number in an infinite set
当使用single-branch clone仓库后,如何添加跟踪的远程分支?
【Leetcode周赛--字符串】6114.移动片段得到字符串
记录记录记录
渲染与云渲染:一部电影的制作25%的时间是在“等”
[in depth study of 4g/5g/6g topic -36]: urllc-7 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -1- business scenarios, evolution routes and performa
Several common methods of database table query in SAP ABAP system
PCIe knowledge point-011: relationship between PCIe configuration capability structure and protocol version
Trojan virus removal method
Day2: language element
这个国产编辑器,即将开源!
关于MySQL的基础学习
类加载机制(类如何被加载)
[深入研究4G/5G/6G专题-36]: URLLC-7-《3GPP URLLC相关协议、规范、技术原理深度解读》-1-业务场景、演进路线和性能要求
Selenium使用之解决‘chromedriver‘ executable needs to be in PATH.报错信息
How to add PTZ control to the easycvr video retrieval page?
国内外知名的待办事项app
微软这个系统,90% 的人都没用过!