Monday, April 28, 2008

Extention Methods(C# 3.0)

Extension Methods : extend new methods without altering the definition of the original type. the extension method must be a static method of a static class, where the this midifier is applied to the first parameter.



class Program
{
static void Main(string[] args)
{
Console.WriteLine("foo".IsCapitalized());
Console.ReadLine();
}
}
public static class StringHelper
{
public static bool IsCapitalized(this string s)
{
if (string.IsNullOrEmpty(s)) return false;
return char.IsUpper(s[0]);
}
}

No comments: