+= Operator with Events

+= operator with Events

+= subscribes to an event. The delegate or method on the right-hand side of the += will be added to an internal list that the event keeps track of, and when the owning class fires that event, all the delegates in the list will be called.

What does it means in C# : using -= operator by events?

Just as += subscribes you a handler to the event, -= unsubscribes it.

Use it when you no longer want a particular handler to be called when the event is raised. You often only need to use it the component raising the event is logically longer lived than the handler of the event - if you don't unsubscribe, the "event raiser" effectively has a reference to the handler, so can keep it alive longer than you want.

As noted in comments:

  • -= will only remove a single handler; if there are multiple handlers subscribed (even using the exact same delegate) it will still only reduce the handler count by 1. The last instance of the specified handler is the one removed. (So if you previously had handlers A, B, A, C subscribed in that order, and removed A, you'd end up with A, B, C.)
  • -= doesn't cause an error if the specified handler is not subscribed to the delegate already; it just ignores the request. This is true even if the event has no handlers subscribed to it at the moment.

Is there any Null-Conditional Operator like for Events.. Button?.Click +=(ss,ee)

The problem is not related to events.

The null-conditional operator is to stop an evaluation if a reference is null.

It is not applicable in the left or right part of an assignment.

If you have:

public class Test
{
public int Value;
public void Method() { }
}

You can't write:

Test v;

v?.Value = 10;

int a = v?.Value;

Because if v is null, v.Value is not evaluated.

  • So what to do with = 10 ?

  • Or what to do with a ?

Thus it is the same when adding or removing an event handler to an event variable that is null when empty.

C# event is null

Hence the compiler error to disallow such writings.

It is why you can't write:

TargetButton?.Click +=(ss,ee)=>{...}

Because what to to with (ss,ee)=>{...} if TargetButton is null?

You can say that you want the compiler ignores that.

But the compiler disallows doing such unclean thing.

What we can write is:

v?.Test();

Here is v is null the method is not called and all is fine because there is nothing right or left that the compiler does not know what to do with.

int a = v?.Value ?? 0;

Here if v is null 0 is used.

Null-conditional operators ?. and ?[]

Null-coalescing operators ?? and ??=

jQuery - Bind two events to an AND operator

It's not possible using syntax like that—the events will never fire at the exact same time, one will always be after the other. That said, you could do something like the following (pseudo code based off your example):

var isHovering = false,
isClicking = false;

function barfunc(e) {
if(isHovering && isClicking){
alert("foobar!");
}
}

foobar.on('mousedown', function(event){

isClicking = true;
barFunc(event);

}).on('mouseup', function(event){

isClicking = false;

}).on('mouseenter', function(event){

isHovering = true;
barFunc(event);

}).on('mouseleave', function(event){

isHovering = false;

});

C# why does assigning an event handler use a += operator and not =

An event can have more than one handler, so it is not a assignment but a subscription.

You could do the following and all handlers would be called.

myButton.Click += MyClickEvent;
myButton.Click += MyClickEvent1;
myButton.Click += MyClickEvent2;

To unsubscribe from the event use

myButton.Click -= MyClickEvent2;

How to generate Events for a certain resource Kubernetes Go Operator

The Kubebuilder v1 book has a good example on how to create and write Events using an EventRecorder from client-go.

See Kubebuilder v1 book - Create Events and Write Events

I want to know if i could declare 2 event listners with OR operator or anything else to load SAME function in just one line

Type is a case-sensitive string representing the event type to listen for. You can not define multiple conditions in it.

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Parameters

But you can apply multiple listeners to the same function like this:

function addMultipleListeners(eventNames) {
var events = eventNames.split(' ');
var eventsLength = events.length;
var i;
for (i = 0; i < eventsLength; i++) {
window.addEventListener(events[i], myFunction);
}
}

addMultipleListeners('mousemove touchmove');

I hope, it helps you.

How does an EventHandler know to allow = operator only in defining class?

Yes, you are correct. The reason for this is that the compiler creates a private delegate object under the covers, like this:

private EventHandler pageOpened;

public EventHandler PageOpened
{
add { pageOpened += value; }
remove { pageOpened -= value; }
}

Inside your class, you have a reference to the private delegate instance, so that's why you can do the assignment. You definitely want to expose a method to clear the targets if that's functionality you need; you don't want to expose the delegate itself.

Overload the += event operator

It's not really overloading, but here is how you do it:

public event MyDelegate SomeEvent
{
add
{
DifferentEvent += value;
AnotherDiffEvent += value;
}
remove
{
DifferentEvent -= value;
AnotherDiffEvent-= value;
}
}

More information on this on switchonthecode.com



Related Topics



Leave a reply



Submit