Reverse String extension on string object in C#

Today I got an idea from a fellow blogger Brad doing a reverse string method in Java here. I thought, lets do this in C#. That would be a nice exercise Smile. So here we go:

public static class StringExtensions

        public static string ReverseString(this string extension)
            string reversedString = "";
            for (int counter = extension.Length -1; counter > -1; counter--)
                reversedString += extension[counter].ToString();

                return reversedString;




The one difference between Brad’s code and mine (other than it being in Java) is that I got a little clever and made it an Extension class. An extension class allows you to extend the functionality of a .NET class ( a third party or your own for that matter).There are just a few things you must do in order to make something an extension class:1.) The class and method must be static2.) The first parameter of the method must use the this keyword and then the class you wish to extend. Thistells the compiler what class you are trying to extend.The meat of this method is fairly simple. All we are doing is starting the counter at the length of the string minus 1 (to account for us starting at an index of 0). Then we subtract 1 each pass through the loop until the counteris no longer greater than –1 or is zero. This stops us from running into any index is out of range errors.To use this method, you would do this:

string data = "I watched the fox run fast";

          data =  data.ReverseString(); 

And that’s all to it..

An introduction to Extensions AND a neat little method!!

Happy Coding!


About Gregg Coleman

I am Senior-level Software Engineer working primarily these days with .NET. I have a good working knowledge of ASP.NET MVC, Web Forms, WCF web services and Windows Services. I spend much of my time in the Web Services (SOAP and REST) world in my current job designing and implementing various SOA architectures. I have been in the software engineering industry for about 6 years now and will not now nor ever consider myself an "expert" in programming because there is always so much to learn. My favorite thing about designing software is there are always new emerging technologies and something to learn every day! My current job has me spending much of my job on the bleeding edge of technologies and changing gears all the time, so I'm never bored and always challenged. On my spare time I enjoy weight training, reading and venturing to new places near by. Of course programing and learning new technologies are another hobby of mine.
This entry was posted in .NET, .NET 4.5, C#, Classes, Computer Technology, Extension Methods, Interfaces, Programming and tagged , , , , , , . Bookmark the permalink.

2 Responses to Reverse String extension on string object in C#

  1. Pingback: LINQ Distinct Enhanced | Object Reference Hell

  2. Pingback: List Extension to detect nulls and more than zero | Object Reference Hell

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s