none
can we declare eventhandler without event keyword in C# RRS feed

  • Question

  • can we declare eventhandler without event keyword in C#?

            event EventHandler<bool> StatusChange1;
             EventHandler<bool> StatusChange2;

    without the event keyword also its working.Then why we have to use event keyword?


    Coding.....................................

    Tuesday, April 9, 2019 11:59 AM

Answers

  • With ‘event’ keyword, you limit the users of your class to only use ‘+=’ and ‘-=’. Otherwise, they will be able to remove the list using ‘StatusChange1 = null’ or to invoke the handlers using ‘StatusChange1(…)’. With ‘event’, such special operations are only allowed for you, inside the class. Without ‘event’, there are no restrictions.


    • Edited by Viorel_MVP Tuesday, April 9, 2019 5:46 PM
    • Marked as answer by La07K Thursday, April 11, 2019 7:11 AM
    Tuesday, April 9, 2019 5:45 PM

All replies

  • You can use

    SomeControl.SomeEvent += 

    Press TAB after = to have Visual Studio create an event. If this is not what you are after please provide more details.

    See also which has several examples.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, April 9, 2019 12:37 PM
    Moderator
  • With ‘event’ keyword, you limit the users of your class to only use ‘+=’ and ‘-=’. Otherwise, they will be able to remove the list using ‘StatusChange1 = null’ or to invoke the handlers using ‘StatusChange1(…)’. With ‘event’, such special operations are only allowed for you, inside the class. Without ‘event’, there are no restrictions.


    • Edited by Viorel_MVP Tuesday, April 9, 2019 5:46 PM
    • Marked as answer by La07K Thursday, April 11, 2019 7:11 AM
    Tuesday, April 9, 2019 5:45 PM
  • Ok...Thank you

    Coding.....................................

    Thursday, April 11, 2019 7:11 AM
  • Thanks for your reply...

    My question is what is the use of event keyword while declaring an eventhandler in C#?


    Coding.....................................

    Thursday, April 11, 2019 7:14 AM