Statics events in C#

Here you will learn about statics events in C#.
  • 2780
Introduction

In this article we will learn about the use of Static's events and the background of the static events in C#. There will be a time when every one wanted to the perform some action in there objects to decrease the requirement of the events handlers so that's why we will use the static events.

Why we use the static events:

In the static events we have use the static modifier and static modifier are used with the property of the program as like the properties ,classes,operators, events etc. for example a class declared as a static and contains only static methods.

Example of the static events:

public class employee
{
public
string id;
public
string name;
public
employee ()
{
}
public employee(string name, string id)
{
this
.name = name;
this
.id = id;
 }
public
static int employeecounter;
public
static int addemployee()
 {
return
++employeecounter;
 }
}
class
Mainclass:employee
{
static
void Main()
{
Console
.Write("enter the employee's name: ");
string name = Console.ReadLine();
Console
.Write("enter the employee's id: ");
string
id = Console.ReadLine();
employee
e = new employee(name, id);
Console
.Write("enter the current number of employee: ");
string n = Console.ReadLine();
employee
.employeecounter = Int32.Parse(n);
employee.addemployee();
Console
.WriteLine("name:{0}", e.name);
Console.WriteLine("id: {0} ", e.id);
Console
.WriteLine ("new number of employee:    {0}" ,employee .employeecounter );
Console.ReadLine();
}
}
}

Output:

statics.jpg

© 2019 DotNetHeaven. All rights reserved.