活动为什么需要委托?为什么我们甚至需要事件? [英] Why do Events need Delegates? Why do we even Need Events?
问题描述
我一直搞不清楚过去几周现在有关事件。我理解代表是如何工作的,它不是如何工作的细节,但不够了解,
委托数据类型
是一个单播委托。
委托无效
是一个多播委托 - 方法的引用清单
我知道一个委托类型编译成一个类,可惜我现在还不能确定如何使用该方法被引用。例如:
委托无效TestDelegate();
TestDelegate testDelegate =新TestDelegate(myObject.SomeMethod);
问题1:我觉得myObject的是目标,并且的someMethod是引用的方法,但我米只传一个输入。
等是myObject.SomeMethod编译为一个字符串,是期间分割字符串?可笑的,我知道。
当你加入一个多播委托
<$ C:
问题2 $ C> multicastdelegate + = newmethodtobereference
multicastdelegate();
在调用列表中的每个方法被调用或通知?
。如果这是真的,为什么在的地狱的我需要的事件或事件
关键字?难道仅仅是为了告诉嘿,这是作为一个事件的开发商?因为我认真困惑,我只是想在此阶段动议的笑。这是一个示例代码,我写今天要测试它是否我需要事件的关键字或不
使用系统;
命名空间LambdasETs
{
公众委托无效IsEvenNumberEventHandler(INT numberThatIsEven);
公共类IsEvenNumberFound
{
公共IsEvenNumberEventHandler IsEvenNumberEvent;
私人诠释数目;
公共无效InputNumber(INT N)
{
如果(编号%2 == 0)
{
如果(IsEvenNumberEvent!= NULL)
{
IsEvenNumberEvent(N);
}
}
}
公共静态无效的主要()
{
IsEvenNumberFound isEvenNumberFound =新IsEvenNumberFound();
isEvenNumberFound.IsEvenNumberEvent + = IsEvenNumberAction;
isEvenNumberFound.InputNumber(10);
到Console.ReadLine();
}
公共静态无效IsEvenNumberAction(INT数)
{
Console.WriteLine({0}为偶数!,数);
}
}
}
添加事件关键字字段公共IsEvenNumberEventHandler IsEvenNumberEvent;
有没有什么区别。
可以请一些解释,这样一个小白能理解表示感谢。
但足够要知道,代表数据类型是单播委托。委托无效是一个多播委托 - 方法的引用清单
块引用>
事实并非如此。所有正常的代表是多播,即使他们有一个非void返回类型
问题1:我觉得myObject的是目标,并且是的someMethod引用的方法,但我只路过一个输入。所以myObject.SomeMethod编译为一个字符串,是期间分割字符串?可笑的,我知道。
块引用>
没有,
myObject.SomeMethod
是一个方法组。代表实例创建的这种方式包含一点点的编译器的魔力。
multicastdelegate + = newmethodtobereference
块引用>
如果
multicastdelegate
是一个正常的代理变量,这相当于multicastdelegate = multicastdelegate + newmethodtobereference
即,它会创建一个新的委托调用的几种方法,并将其分配给multicastdelegate
。
现在你的主要问题:什么是事件的目的
事件有委托类型。它们的行为类似于属性。他们的目的就是封装,特别是他们只让消费者以认购(
+ =
)和退订(- =
),但。不要看事件的值
属性是两种方法的组合:
GET
和设置
。
事件是两个公共方法的组合
订阅
和取消
,并在现场般的事件也是类似的私人吸气东西的情况。I've been confused over the past weeks now about events. I understand how delegates work, not how it works in detail but enough to know that
delegate datatype
is a single cast delegate.delegate void
is a multicast delegate - a list of references to methods.I know a delegate type compiles to a class, but unfortunately I am still not sure how the method is referenced. For example
delegate void TestDelegate(); TestDelegate testDelegate = new TestDelegate(myObject.SomeMethod) ;
Question 1: I think myObject is the target, and SomeMethod is the method to reference, but I'm only passing one input. So is myObject.SomeMethod compiled to a string and is the string split by the period? Ridiculous I know.
Question 2: When you add to a multicast delegate
multicastdelegate+=newmethodtobereference multicastdelegate() ;
Every method in the invocation list is called or notified? If that's true, why the hell do I need events or the
event
keyword? Is it simply to tell the developers that Hey, this is acting as an event? Because I'm seriously confused, I just want to move on at this stage lol. This is a sample code I wrote to test it today whether I need event keyword or not.using System; namespace LambdasETs { public delegate void IsEvenNumberEventHandler(int numberThatIsEven); public class IsEvenNumberFound { public IsEvenNumberEventHandler IsEvenNumberEvent; private int number; public void InputNumber(int n) { if(number %2 ==0) { if (IsEvenNumberEvent != null) { IsEvenNumberEvent(n); } } } public static void Main() { IsEvenNumberFound isEvenNumberFound = new IsEvenNumberFound(); isEvenNumberFound.IsEvenNumberEvent += IsEvenNumberAction; isEvenNumberFound.InputNumber(10); Console.ReadLine(); } public static void IsEvenNumberAction(int number) { Console.WriteLine("{0} is an even number!", number); } } }
Adding the event keyword to the field
public IsEvenNumberEventHandler IsEvenNumberEvent;
has no difference. Please can some explain so that a noob can understand thanks.解决方案but enough to know that delegate datatype is a single cast delegate. delegate void is a multicast delegate - a list of references to methods.
Not true. All "normal" delegates are multicast, even if they have a non void return type.
Question 1: I think myObject is the target, and SomeMethod is the method to reference, but I'm only passing one input. So is myObject.SomeMethod compiled to a string and is the string split by the period? Ridiculous I know.
No,
myObject.SomeMethod
is a method group. This way of delegate instance creation involves a bit of compiler magic.multicastdelegate+=newmethodtobereference
If
multicastdelegate
is a normal delegate variable, this is equivalent tomulticastdelegate = multicastdelegate + newmethodtobereference
i.e. it creates a new delegate that calls several methods, and assigns it tomulticastdelegate
.
Now to your main question: What's the purpose of events?
Events have delegate types. They behave similarly to properties. Their purpose is encapsulation, in particular they only allow consumers to subscribe(
+=
) and unsubscribe(-=
) but not to read the value of the event.Properties are a combination of two methods:
get
andset
.Events are a combination of two public methods
subscribe
andunsubscribe
, and in the case of a field-like event also something similar to a private getter.这篇关于活动为什么需要委托?为什么我们甚至需要事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!