using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ExtensionExample { class ExtensionExample { static void Main(string[] args) { Console.WriteLine("ExtensionExample.cs"); Console.Write("Enter a number between 1 and 10 ==> "); int i = int.Parse(Console.ReadLine()); Console.WriteLine("The value is {0} ", i.CheckRange(1, 10) ? i.ToString() : "out of range"); i. Console.Write("Enter of these values {CA | NY | TX | FL | NC} ==> "); String state = Console.ReadLine().ToUpper(); Console.WriteLine("The state is {0} ", state.CheckValues("CA", "NY", "TX", "FL", "NC") ? state : " not in the list"); Console.WriteLine("End of tester"); Console.ReadLine(); } } /// /// Class that contains commonly used "check" extensions to /// perform DDS-like checking of data types. /// /// /// The class that contains extension methods must be a static class. /// The methods within the class are static methods. /// public static class CheckExtensions { /// /// Extension method that adds a range checking feature to integer data. /// /// The integer value to check. /// The inclusive low value of the range to check. /// The inclusive high value of the range to check. /// Boolean indicating whether or not the value is within the range. public static bool CheckRange(this int number, int lowValue, int highValue) { if ((number >= lowValue) && (number <= highValue)) { return true; } else { return false; } } /// /// Extension method that adds a list of values checking feature to string data. /// /// The string value to check. /// A comma-delimited list of string values that are the /// valid values. /// Boolean indicating whether or not the value is in the list. public static bool CheckValues(this String testValue, params String[] values) { if (values.Contains(testValue)) { return true; } else { return false; } } } }