问题描述
我读过两本书,有很多例子.他们对我来说仍然几乎没有意义.我可能会编写一些使用委托的代码,但我不知道为什么.我是唯一有这个问题的人,还是我只是个白痴?如果有人能真正向我解释我何时、何地以及为什么我会实际使用代理,我会永远爱你.
I've read two books, tons of examples. They still make next to no sense to me. I could probably write some code that uses delegates, but I have no idea why. Am I the only one with this problem, or am I just an idiot? If anyone can actually explain to me when, where, and why I would actually use a delegate, I'll love you forever.
推荐答案
委托只是在变量中传递函数的一种方式.
Delegates are just a way to pass around a function in a variable.
您传递一个委托函数来执行回调.比如在做异步 IO 的时候,你传递一个委托函数(一个你用委托参数写的函数),当数据从磁盘读取出来时,它会被调用.
You pass a delegated function to do a callback. Such as when doing asynchronous IO, you pass a delegated function (a function you have written with the delegate parameter) that will be called when the data has been read off the disk.
这篇关于为什么试图理解代表感觉就像试图理解宇宙的本质?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!