C# 3.0 introduced the concept of extension methods. By using this feature, we can add various utility methods to the CLR types. few times, in our application we wants to check whether the value entered by user is numeric or not. Here is an example of adding an extension method to string type and checking it.
public static class Extensions { public static bool IsNumeric(this string str) { Regex _isNumber = new Regex(@"^\d+.\d+$"); Match m = _isNumber.Match(str); return m.Success; } }
string valtotest = "123.12"; Response.Write(valtotest.IsNumeric());
Published under: Microsoft .NET Tips · · · ·