How to implement Extension Methods?
Extension methods can be implemented using static class, static method and the this keyword. The code to implement Extension methods is shown below
public static class MyExtensionMethods
{
///<summary>
/// to check numeric or not
///</summary>
///<param name="s"></param>
///<returns></returns>
public static bool IsNumeric(this string s)
{
float output;
return float.TryParse(s, out output);
}
///<summary>
/// Converts Enumeration type into a dictionary of names and values
///</summary>
///<param name="t"></param>
///<returns></returns>
public static IDictionary<string, int> EnumToDictionary(thisType t)
{
if (t == null) throw new NullReferenceException();
if (!t.IsEnum) throw new InvalidCastException("object is not an Enumeration");
string[] names = Enum.GetNames(t);
Array values = Enum.GetValues(t);
return (from i in Enumerable.Range(0, names.Length)
select new { Key = names[i], Value = (int)values.GetValue(i) })
.ToDictionary(k => k.Key, k => k.Value);
}
}

Join the conversation! Your thoughts help the community grow.