Events in c#

Hi Friends,

 
In the below post you will learn about how to define events, event handler and how to pass custom event argument to the event handler in C #.

Sometimes while coding you get the situation like you want to notify another objects when something related to that objects happen. 

 
For example, Support We have a book publisher object and couple of book lovers. The book lovers object want to be notified whenever a new book published by the book publisher. In this situation we can go for “Events”.  Lets go through a example code as below.
 
using System;
 
namespace EventExample
{
    public class Book
    {
        public string BookName { get;  set; }
        public DateTime PublishDate { get;  set; }
        public string ISBN { get;  set; }
        public string Authors { get;  set; }
        
    }
 
    // Custom event argument for passing extra information
    public class BookInfoEventArgs : EventArgs
    {
        public Book bookInfo { get; private set; }
        public BookInfoEventArgs(Book book)
        {
            this.bookInfo = book;
        }
    }
 
    public class BookPublisher
    {
         public event EventHandler<BookInfoEventArgs> BookSubscibers;
 
        public void PublishNewBook(Book book)
        {
            if (BookSubscibers != null)
            {
                BookSubscibers(this, new BookInfoEventArgs(book));
            }
        }
    }
 
    public class BookLover
    {
        public string Name { get; private set; }
 
        public BookLover(string name)
        {
            this.Name = name;
        }
 
        public void NewBookPublished(object sender, BookInfoEventArgs e)
        {
            Console.WriteLine(“{0}, New Book Published, Here is the book information: \n” +
            “Book Name :{1}\nPublish Date: {2}\nISBN : {3}\nAuthor: {4}\n\n”, this.Name, e.bookInfo.BookName, e.bookInfo.PublishDate, e.bookInfo.ISBN, e.bookInfo.Authors);
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            var publisher = new BookPublisher();
 
            var customer1 = new BookLover(“Mahesh”);
            publisher.BookSubscibers += customer1.NewBookPublished;
 
            var costomer2 = new BookLover(“Madan”);
            publisher.BookSubscibers += costomer2.NewBookPublished;
 
            //Publish new Book
            publisher.PublishNewBook(new Book
            {
                BookName = “Learn C++”,
                ISBN = “HJNJJJ782332HH”,
                PublishDate = DateTime.Now,
                Authors = “Ramesh”
            });
 
 
            Console.ReadLine();
        }
    }
}
 
 
In the above code I have defined the “Book” class to hold book related information. Then I have defined a “BookInfoEventArgs” class which is custom event argument class. This class will help us passing the new book related information to the book lovers. The “BookPublisher” is another class which responsibility is to notify “BookLover” and that is the next class.
 
Now lets re-visit the main method . First I have defined a publisher object and then I have defined a customer1 object. in the next line I have added the costomer1.NewBookPublished event handler to the “publisher.BookSubscibers” delegate. In the next line of code I have defined another object customer2 and did the some thing.
 
Now when we execute this code the publisher.PublishNewBook method will be triggered after the above assignments. This method pass the new book information to the “publisher” instance. if you check the “PublishNewBook ” method then you will see below line of code.
 
if (BookSubscibers != null)
 {
                BookSubscibers(this, new BookInfoEventArgs(book));
 }
 
This line of code is basically notifying every subscriber. i,e, calling every method which we have added to the delegate “BookSubscibers” which are :
 
customer1.NewBookPublished
customer2.NewBookPublished
 
After executing the above code example you may get the below console output.
 
 

 


 
 
 
In the above post you have learned about how to define events, event handler and how to pass custom event argument to the event handler. in C #
 
Thanks for reading . Happy Coding. Emoji
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s