Extension Methods in C#

With help of extension method you can add a new method to an existing type without recompile and modifying existing type.

e.g.

string s = “hello”; string i = s.UpperCasefirstLater();

Result : Hello

Here UpperCasefirstLater is a extension method. see below code
public static class MyExtensions
{
public static string UpperCasefirstLater(this String str)
{
if (str.Length > 0)
{
char[] array = str.ToCharArray();
array[0] = char.ToUpper(array[0]);
return new string(array);
}
}
}

Leave a comment