Sign In
  
  
  
  



  

 

 

string.Reverse and C# 2.0 vs C# 3.0

 
 
Description:

This post is from Channel 8.  It was originally posted on 7/7/07 by Christian Liensberger.

I have been reading some Ruby tutorials and seen that Ruby has a Reverse method defined on string. Now I thought, why not create something like that for C#. My first idea was to add this to C# 2.0.

The first thing would be to create a static utility class (you can't inherit from string in .NET) and implement a static Reverse method on it. Since I'm in C# 2.0 I'm thinking in a very procedural way:

// Utility class for System.String.
public static class StringUtility
{
    // Reverses the given string.
    public static string Reverse(string value)
    {
        if (value == null)
            throw new ArgumentNullExc­eption("value")­;
        if (value.Length == 0)
            return value;

        // Create the input and output array.
        char[] input = value.ToCharArr­ay();
        char[] output = new char[value.Leng­th];

        // Loop over the array and move the chars.
        for (int i = 0; i < input.Length; i++)
        {
            output[input.Le­ngth - 1 - i] = input[i];
        }

        // Create the new output string.
        return new string(output);
    }
}

Now, how would that look like in C# 3.0? C# 3.0 adds some new functional sugar to the language. It allows you to specify lambda expressions as functional statements. Internally C# creates delegates for these lambda expressions. These delegates are then invoked, as the code runs. The new version of the language adds also so called extension methods. Extension methods are defined in own classes and extend an existing class, struct or interface. If an extension method for string has been defined it shows up in IntelliSense, when you work with a string and the class defining the extension method is in scope. The compiler is then translating the extension methods to normal method calls!

 

Defining an extension method is very easy. It only requires the developer to put a this keyword as the first argument of the method. That keyword signalizes that the extension method is defined for the argument's type.

Let's have a look at the code:

// Class that holds the extensions for string.
public static class StringExtension­s
{
    // Reverses the given string.
    public static string Reverse(this string value)
    {
        // Create the function definition. 
        // This is required since we do a recursive call.
        Func f = null;
        // Create the lambda expression.
        f = x => x.Length > 0 ? f(x.Substring(1­)) + x[0] : 
            string.Empty;

        // Invoke it.
        return f(value);
    }
}

That's shorter, isn't it? The lambda expression looks a little bit complexer, although it's very easy. You only need to understand that the thing before the => is the argument of the function and on the right side of the => follows a simple ternary operation that makes clear that we exit when the whole string has been processed.

 

Report abuse for blog post  |  Share
 
 

Related Posts