In this post, I tried to explain the string extension method with an example.
Most of the time, we are in the position to call the extension method by knowing or without knowing this as extension methods. In our coding life, we keep using the extension methods regularly. We know very well that the .NET Framework comes with a set of predefined classes, functions, and properties. In below example, I will explain how to create an extension method.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ExtensionMethodDemo
{
public static class StringExtension
{
public static String ChangeFirstLetterCase(this String stringToChange)
{
char[] charstr = stringToChange.ToCharArray();
charstr[0] = char.IsUpper(charstr[0]) ? char.ToLower(charstr[0]) : char.ToUpper(charstr[0]);
return new string(charstr);
}
}
class Program
{
static void Main(string[] args)
{
string Str = "programming Trends";
string Result = Str.ChangeFirstLetterCase();
//string Result = StringExtension. ChangeFirstLetterCase(Str);
Console.WriteLine(Result);
Console.ReadKey();
}
}
}
0 comments:
Post a Comment
Please do not enter any spam link in the message box.