CSharp examples for Custom Type:Extension Methods
Demonstrating extension methods.
using System; // for class ArgumentOutOfRangeException public class Time2 { private int hour; // 0 - 23 private int minute; // 0 - 59 private int second; // 0 - 59 // constructor can be called with zero, one, two or three arguments public Time2(int hour = 0, int minute = 0, int second = 0) {//w w w .jav a 2 s . c o m SetTime(hour, minute, second); // invoke SetTime to validate time } // Time2 constructor: another Time2 object supplied as an argument public Time2(Time2 time) : this(time.Hour, time.Minute, time.Second) { } // set a new time value using universal time; ensure that // the data remains consistent by setting invalid values to zero public void SetTime(int hour, int minute, int second) { Hour = hour; // set the Hour property Minute = minute; // set the Minute property Second = second; // set the Second property } // property that gets and sets the hour public int Hour { get { return hour; } set { if (value < 0 || value > 23) { throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Hour)} must be 0-23"); } hour = value; } } // property that gets and sets the minute public int Minute { get { return minute; } set { if (value < 0 || value > 59) { throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Minute)} must be 0-59"); } minute = value; } } // property that gets and sets the second public int Second { get { return second; } set { if (value < 0 || value > 59) { throw new ArgumentOutOfRangeException(nameof(value), value, $"{nameof(Second)} must be 0-59"); } second = value; } } // convert to string in universal-time format (HH:MM:SS) public string ToUniversalString() => $"{Hour:D2}:{Minute:D2}:{Second:D2}"; // convert to string in standard-time format (H:MM:SS AM or PM) public override string ToString() => $"{((Hour == 0 || Hour == 12) ? 12 : Hour % 12)}:" + $"{Minute:D2}:{Second:D2} {(Hour < 12 ? "AM" : "PM")}"; } class MainClass { static void Main() { var myTime = new Time2(); // call Time constructor myTime.SetTime(11, 34, 15); // set the time to 11:34:15 // test the DisplayTime extension method Console.Write("Use the DisplayTime extension method: "); myTime.DisplayTime(); // test the AddHours extension method Console.Write("Add 5 hours with the AddHours extension method: "); var timeAdded = myTime.AddHours(5); // add five hours timeAdded.DisplayTime(); // display the new Time2 object // add hours and display the time in one statement Console.Write("Add 15 hours with the AddHours extension method: "); myTime.AddHours(15).DisplayTime(); // add hours and display time // use fully qualified extension-method name to display the time Console.Write("Use fully qualified extension-method name: "); TimeExtensions.DisplayTime(myTime); } } // extension-methods class static class TimeExtensions { // display the Time2 object in console public static void DisplayTime(this Time2 aTime) { Console.WriteLine(aTime.ToString()); } public static Time2 AddHours(this Time2 aTime, int hours) { // create a new Time object var newTime = new Time2() { Minute = aTime.Minute, Second = aTime.Second}; // add the specified number of hours to the given time newTime.Hour = (aTime.Hour + hours) % 24; return newTime; // return the new Time2 object } }