String class reverse function and extension method
Posted on 10/7/2007 9:07:31 AM
in #Asp.Net 3.X
Hi,/P>
One of the most basic functions that I would like to add with the help of the new extension method in dot net 3.5 is the reverse method in the string class. Many a time we want to reverse the character in the string. Currently we do not have any inbuilt method in the framework to reverse the character in a string.
Yes till now we had to work with a static method to reverse the string, but I would love to have this functionality in the string class itself. Here is the implementation of the same.
public static void Reverse(this string strObj)/P>
{/P>
/SPAN>char[] characters = strObj.ToCharArray();/P>
/SPAN>Array.Reverse(characters);/P>
/SPAN>strObj = new string(characters);/P>
}/P>
Now we can reverse the string very easily like this.
string toReverse = “Reverse theString”;/P>
toReverse. Reverse();/P>
I am really starting to like this extension method and in the process creating my own set that I will add in the current framework classes. I am having lots and Lotys of ideas on where I will be using them. Do share you ideas on the extension method :-)
Vikram/P>
|