Skip to main content

Csharp Events basics


Events in C#

Syntax of event declaration
[attributes][modifers] event <delegate_type> <event_name>[ {event body}   ];


we will discuses about syntax of event


·        Attributes are csharp attributes
·        Modifiers are optional and those may one are combination of more than one
o   Accessing specifies
§  Public, private, protected, internal
§  New, Virtual, Abstract, override
§  Static ,extern
o   Event will work between objects because of that when your are defining event in main function existing class you have to specifies event as static             
·        Event
o   It is a keyword used to give event declaration
o   In syntax event keyword must not be omitted
·        Type
o   Event is a role player  variable foe delegate
o   Without delegate event will not fired
o   Type is a delegate type which is giving functions to event at run time when event is fired this process is known as hookup
·        Event name
o   It is identifiers or event variable name
o   It must be unique in program
·        Accessor _declaration
o   When programmer want add or remove  functionalities to a particular events ,specification will be done in body
o   We can do two way
o   With add and remove


using System;

namespace eventdemo1

{
    public delegate void eventdel();
   
    class Program
    {
        public static event eventdel myevent;

        public static void eventfire_switch() { myevent.Invoke(); }


        public static void f (){ Console.WriteLine("i was fired by myevent");}

        public static void f2() { Console.WriteLine("i was added and removed at 2 fired event by myevent"); }
        static void Main(string[] args)
        {
            myevent += new eventdel(f);
            myevent += new eventdel(f2);
            eventfire_switch();
         
        }
    }
}


FAQS

  1. what is event in chsharp
  2. what is difference between events and delegates in chsharp
  3. syntax for event in chsharp
  4. what is type of events in chsharp
  5. can i write static events in csharp
  6. list out attributes of events in chsharp
thank you

Comments

Popular posts from this blog

connection pooling

Explain about Connection Pooling? Connection pooling is a concept which deals with database connection. A Connection Pool maintain last database connection active state when connection is ideal. Whenever you want to make a transaction with database once again you have to open connection when it is ideal, instead of that connection pooling will maintain open and reusable connections. A Connection Pool is released from the buffer or memory when the last connection to the database is closed. By default Connection Pooling turned on in ado.net; whenever you want to change it off, you have to specify Pooling = false in the connection string . We can specify limit to connection pooling by default the maximum limit is 100. You will specify in connection string. It will improves the performance and scalability of the application It is managed by a program in ado.net that we called as Pool Manager. This program will ma...

Shortcut keys inMSDOTNET 2015

when ever we want work with MS.net we have to learn about some keys which can help to improve our work force in development area .i  got some shortcut key in msdn ,You can view all of the keyboard shortcuts for Visual Web Developer. we can create our own shortcuts by doing the following:  on the  Tools  -->  Options ; it will open dialog box  of the Options ,  select  Environment  and then  Keyboard . we have to select the  Show all settings  check box in order to view the  Keyboard  setting. Shortcut Description CTRL+B Switches the selected text between bold and normal. CTRL+I Switches the font style of the selected text between italic and roman. CTRL+U Switches the font style of the selected text between underline and roman. CTRL+SHIFT+L Displays the  Bookmark  dialog box. CTRL+L When text is selected, displays the  Hyperlink  dialog box. ...

Fundamentals of Design Patterns

Fundamentals of Design Patterns   hi developers and programmers  in this post i am going to share Videos where i learned fundamentals of design patterns   it is great visual you can enjoy this  have a nice day