I do not understand the difference between a clean delegation field and fields

Delegate: I understand. But when I get to the event, I don’t understand many things. I read a book, MSDN and some simple examples on the Web, they both have the same structure. For example, here is the link: Event Example

I take the first example that the author said that this is the simplest example about a C # event.

Here is his code:

public class Metronome
{
    public event TickHandler Tick;
    public EventArgs e = null;
    public delegate void TickHandler(Metronome m, EventArgs e);
    public void Start()
    {
        while (true)
        {
            System.Threading.Thread.Sleep(3000);
            if (Tick != null)
            {
                Tick(this, e);
            }
        }
    }
}

public class Listener
{
    public void Subscribe(Metronome m)
    {
        m.Tick += new Metronome.TickHandler(HeardIt);
    }
    private void HeardIt(Metronome m, EventArgs e)
    {
        System.Console.WriteLine("HEARD IT");
    }
}

class Test
{
    static void Main()
    {
        Metronome m = new Metronome();
        Listener l = new Listener();
        l.Subscribe(m);
        m.Start();
    }
}

You can see the line: public event TickHandler Tick. When I switch to public TickHandler Tick, the program still works the same. But I understand the new line, because it is just a pure delegate.

So, my question is: what is the real purpose of the keyword eventin the string: public event TickHandler Tick. This is very important because all examples always use this, but I cannot explain why.

Thank:)

+5
7

- , . "" ( ):

  • , . ( , .)
  • , , , "", .

, . - add/remove, . foo.SomeEvent += handler; foo.SomeEvent -= handler;.

, get/set (, , ).

, :

public event TickHandler Tick;

, :

private TickHandler tick;

public event TickHandler
{
    add { tick += value; }
    remove { tick -= value; }
}

, - , . , .

, , - (IMO) , , event "" - , - . , , , .

// Not real C#, but I wish it were...
public event TickHandler Tick { add; remove; }

, , .

+9

event delegate. =.

( +=) ( -=) . , "" .

, : m.Tick = new Metronome.TickHandler(HeardIt)

+7

"event" . ?

  • , ,
  • add remove,
  • SomeMethod(object source, EventArgs args), .
+3

- event . , , . event , , , , . , ( ).

+1

, ,

. m.Tick + =

, ( ), + =. Tick TickHandler, , TickHandler.

, int.

string stringTest = string.Empty;
stringTest += "this works";
stringTest += 4; //this doesn't though
int intTest = 0;
intTest += 1; //works because the type is the same
intTest += "This doesn't work";
Metronome m = new Metronome();
Metronome.TickHandler myTicker = new Metronome.TickHandler(function);
m.Tick += myTicker; //works because it is the right type
m.Tick += 4; //doesn't work... wrong type
m.Tick += "This doesnt work either"; //string type is not TickHandler type

?

+1

, - , , ​​ , .

:

= <

/ + = - =

()

              Operation         | delegate   | event
              ------------------+------------+--------
Inside class  += / -=           | valid      | valid
              ------------------+------------+--------
Inside class  =                 | valid      | valid
              ------------------+------------+--------
Inside class  ()                | valid      | valid
              ------------------+------------+--------
Outside class  += / -=          | valid      | valid
              ------------------+------------+--------
Outside class  =                | valid      | not valid
              ------------------+------------+--------
Outside class  ()               | valid      | not valid

, .: -)

+1

I think the main difference between using a delegate and an event is that the event can only be raised by the server (means class author)

If you delete the keyword event, now you can raise m.Tick(sender,e)in a Listenerdifferent way.

public class Listener
{
  public void Subscribe(Metronome m)
  {
    m.Tick += new Metronome.TickHandler(HeardIt);
  }

  private void RaisTick(object sender, EventArgs e)
  {
      m.Tick(sender,e);
  }
  private void HeardIt(Metronome m, EventArgs e)
  {
     System.Console.WriteLine("HEARD IT");
  }

}

0
source

All Articles