String class reverse function and extension method

Hi,

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)

{

    char[] characters = strObj.ToCharArray();

    Array.Reverse(characters);

    strObj = new string(characters);

}

Now we can reverse the string very easily like this.

string toReverse = “Reverse theString”;

toReverse. Reverse();

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


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2009 Vikram Lakhotia