Frage

I'm reading a book on C#, and it has this to say about compound assignments (e.g. +=, -=, *=, /=, <<=, >>=):

A subtle exception to this rule is with events, which we describe in Chapter 4: the += and -= operators here are treated specially and map to the event's add and removed accessors.

Can anyone explain what that means in plain English? I'm not to Chapter 4 yet.

War es hilfreich?

Lösung

Normally a += would add the expression/variable on the right hand side to the one on the left and assign the result to the left hand side.

// if a = 4, after this statement, a would be 5
a += 1;

But in case the left hand side of the expression with a += is an event, then this is not the case, but it would be the event handler on the right hand side, which is added to the list of event handlers for that event.

// whereas on the below statement someEventHandler is added to the collection of event handlers for the 'OnSomeEvent' event
self.OnSomeEvent += someEventHandler

Andere Tipps

It simply means += is attaching an event method to e.g. a control instead of addition (e.g. arithmetic addition)

// += as in arithmetic addition
int i = 3;
i += 1;  // i now outputs 4

// += as in programmatically attaching an event to a control 
Button btn = new Button();
btn.Click += new EventHandler(btn_Click); 

private void btn_Click(object obj) { ... ; }

see the difference?

There is a notion of delegate in C# which can point a Method. You can think of events like a special type of delegates. You can add (or remove) many methods to an event. It allows you to execute specified methods when a specific event occurs.

A simple example when you delete a file that shows delete result both on Console and MessageBox.

// This is the delegate. Any instance with DeletedEventHandler type 
// can point a method which returns voids and accepts parameters (object,bool)
public delegate void DeletedEventHandler(object sender, bool deleted);

public class FileDeleter
{   
     private DeletedEventHandler onDelete;

     public event DeletedEventHandler OnDelete
     {
         // The add and remove accessors
         add { onDelete += value; }
         remove { onDelete -= value; }
     }

     public void Delete(string filePath)
     {
         try
         {
             File.Delete(filePath);
             RaiseOnDelete(true);
         }
         catch
         {
             RaiseOnDelete(false);
         }
     }

     private void RaiseOnDelete(bool deleted) 
     {
         if (onDelete != null)
             onDelete(this, deleted); // all methods added executes here
     }
}


public void TestMethod()
{
    FileDeleter deleter = new FileDeleter();
    // Note that both ShowDeleteInfoWindows and ShowDeleteInfoConsole are in form of void(object,bool) which is compatible with DeletedEventHandler 
    deleter.OnDelete += new DeletedEventHandler(DeletedOnConsole);
    deleter.OnDelete += new DeletedEventHandler(ShowDeleteInfoWindows);
    deleter.Delete(@"C:\test.txt");
}

private void ShowDeleteInfoConsole(object deleter, bool isDeleted)
{
    Console.WriteLine(isDeleted);
}

private void ShowDeleteInfoWindows(object deleter, bool isDeleted)
{
    MessageBox.Show(isDeleted.ToString());
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top