In C#, you can extend the functionality of Enumerated type by using extension method same as for other types. Here , I will show you a simple example of creating extension method for Enumerated type.
In my previous posts, I explained Check Internet Connection using C#, SQL Server Database BackUp using C#, Partial Methods, Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server .
Below, I have created an extension method for DayofWeek enumarated type.
Now you can call ToDoAlert method on any variable on constant of type DayOfWeek as shown below.
In my previous posts, I explained Check Internet Connection using C#, SQL Server Database BackUp using C#, Partial Methods, Contextual Keyword, C# Static Methods and some other articles related to C#, ASP.Net and SQL Server .
Below, I have created an extension method for DayofWeek enumarated type.
/// <summary>
/// Creating Extension Method for Enumarated Type
/// </summary>
/// <param name="day"></param>
/// <returns></returns>
public static string ToDoAlret(this DayOfWeek day)
{
string strAlert = string.Empty;
switch (day)
{
case DayOfWeek.Sunday:
strAlert = "Cricket";
break;
case DayOfWeek.Monday:
strAlert = "Reading";
break;
case DayOfWeek.Tuesday:
strAlert = "Meeting";
break;
case DayOfWeek.Wednesday:
strAlert = "Club";
break;
case DayOfWeek.Thursday:
strAlert = "Movie";
break;
case DayOfWeek.Friday:
strAlert = "Clebrating";
break;
case DayOfWeek.Saturday:
strAlert = "Logn Drive";
break;
}
return strAlert;
}
Now you can call ToDoAlert method on any variable on constant of type DayOfWeek as shown below.
//Calling the ToDoAlert method of type DayOfWeek
DayOfWeek today = DayOfWeek.Saturday;
Console.WriteLine(today.ToDoAlert());
Console.WriteLine("---------------");
Console.WriteLine(DayOfWeek.Sunday.ToDoAlert());
Console.ReadKey();
I hope you enjoyed it. I would like to have any feedback from you. Your valuable feedback, question, or comments about this article are always welcome.
No comments:
Post a Comment