using
System;
using
System.Collections.Generic;
namespace
GFG {
public
class
Video {
public
string
Name;
}
public
interface
IChannel {
event
EventHandler VideoAdded;
}
public
class
Channel : IChannel
{
public
event
EventHandler VideoAdded;
public
string
Name
{
get
;
set
;
}
public
List<Video> ListOfVideos =
new
List<Video>();
public
void
AddVideo()
{
Video video =
new
Video();
video.Name =
"EventHandler in c#"
;
ListOfVideos.Add(video);
NotifyAllSubscribers();
}
public
void
NotifyAllSubscribers()
{
EventHandler handler = VideoAdded;
if
(handler !=
null
) {
handler(
this
,
new
EventArgs());
}
}
}
public
class
Subscriber_A {
public
string
Name
{
get
;
set
;
}
public
Subscriber_A(Channel ch)
{
ch.VideoAdded
+=
new
EventHandler(ReceiveVideoNotification);
}
public
void
ReceiveVideoNotification(
object
sender,
EventArgs e)
{
Console.WriteLine(
"Hello {0}, a
new
video has
been uploaded
by
GeeksForGeeks",
Name);
Console.WriteLine();
}
}
public
class
Subscriber_B {
public
string
Name
{
get
;
set
;
}
public
Subscriber_B(Channel channel)
{
channel.VideoAdded
+=
new
EventHandler(ReceiveVideoNotification);
}
public
void
ReceiveVideoNotification(
object
sender,
EventArgs e)
{
Console.WriteLine(
"Hello {0}, a
new
video has been
uploaded
by
GeeksForGeeks",
Name);
Console.WriteLine();
}
}
class
Program {
static
void
Main(
string
[] args)
{
Channel channel =
new
Channel();
channel.Name =
"GeeksForGeeks"
;
Subscriber_A A =
new
Subscriber_A(channel);
A.Name =
"Geek_1"
;
Subscriber_B B =
new
Subscriber_B(channel);
B.Name =
"Geek_2"
;
channel.AddVideo();
}
}
}