Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0219 Extension method

Extension methods add new functions to existing types. Sometime it can be system types. For example, you can add extension method to string type. Extension methods must stay in static class and must be static method itself. The first parameter is marked by this keyword. using System; public static class StringHelper { public static bool IsCapitalized(this string s) { if (string.IsNullOrEmpty(s)) return false; return char.IsUpper(s[0]); } } class Test { static void Main() { string str = "rntsoft.com"; Console.WriteLine(str.IsCapitalized()); } } The output: False