Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# events with custom event accessor (add and remove)

Tags:

c#

Can someone explain to me how custom event accessor works? I have read a couple of articles on google but still could not understand. Also, when to use it?

I have the following code snippet from my project. If someone can explain this to me it would be really great.

private event Action<int> ActivityChanged = delegate {};

event Action<int> IActivityFacade.ActivityChanged
{
    add
    {
        ActivityChanged += value;
        value(GetSelectedActivity()); 
    }
    remove { ActivityChanged -= value; }
}
like image 392
App Avatar asked Jul 17 '16 04:07

App


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


1 Answers

Without knowing exactly what part of the existing documentation and other references regarding custom event accessors it is specifically that you don't understand, it's impossible to know for sure what answer you're looking for.

A C# event is simply an add and a remove method (similar to a property's get and set methods). The compiler provides a default implementation for these methods, but if you have custom behavior you want to implement yourself, you can write the methods yourself. In your example, which is not thread-safe by the way, the custom accessors are apparently there so that a newly subscribed delegate is invoked as soon as it's added.

If you need more details than that, please improve the question so that it's clear what it is specifically about custom event accessors that you are having trouble understanding. Be sure to explain exactly what you do understand and what documentation you've already referenced, so that we can avoid excessively long answers that waste time on aspects you already know about.

like image 62
Peter Duniho Avatar answered Oct 14 '22 10:10

Peter Duniho