Posted by: Zeeshan Amjad | October 25, 2011

Recursive extension method


We have seen different examples of extension method. Now we are going to see how can we make extension method recursive, i.e call itself. We saw few example of recursive function using C++ lambda here. Now we are going to see how we can do the same thing with extension method.

Writing recursive method is not very difficult. We only have to take care the current value of the number on which we are calling the extension method and due to this sometime we have to create a temporary variable.

Here is recursive extension function to calculate the factorial

Code Snippet
public static int Factorial(this int number)
{
    if (number == 0)
    {
        return 1;
    }
    else
    {
        number–;
        return (number + 1)  * number.Factorial();
    }
}

 

Note that here we have to subtract the number first before calling it recursively, otherwise it will call the function with the same value.

Here is recursive Fibonacci method. Here we have to create two temp variable because we are calling recursive function twice.

Code Snippet
public static int Fibonacci(this int number)
{
    if (number < 1)
        return -1;

    if (number == 1 || number == 2)
        return 1;
    else
    {
        int temp1 = number – 1;
        int temp2 = number – 2;

        return temp1.Fibonacci() + temp2.Fibonacci();
    }

 

Next example shows the nested call of recursive method. It is an implementation of Ackermann function.

Code Snippet
public static int Ackermann(this int m, int n)
{
    if (m < 0 || n < 0)
        return -1;

    if (m == 0)
        return n + 1;
    else if (m > 0 && n == 0)
    {
        m–;
        return m.Ackermann(n);
    }
    else
    {
        int temp = m – 1;
        return temp.Ackermann(m.Ackermann(n – 1));
    }
}

 

We can even pass the default parameter to the recursive method. Here is one such example.

Code Snippet
public static int ReverseNumber(this int number, int r = 0)
{
    if (number == 0)
        return r;
    else
    {
        int temp = number / 10;
        return temp.ReverseNumber((r * 10) + (number % 10));
    }
}

 

Here is complete C# code of the static class that contains all of these recursive extension methods.

Code Snippet
public static class MyExtensions
{

    public static int Factorial(this int number)
    {
        if (number == 0)
        {
            return 1;
        }
        else
        {
            number–;
            return (number + 1)  * number.Factorial();
        }
    }

    public static int Fibonacci(this int number)
    {
        if (number < 1)
            return -1;

        if (number == 1 || number == 2)
            return 1;
        else
        {
            int temp1 = number – 1;
            int temp2 = number – 2;

            return temp1.Fibonacci() + temp2.Fibonacci();
        }
    }

    public static int Ackermann(this int m, int n)
    {
        if (m < 0 || n < 0)
            return -1;

        if (m == 0)
            return n + 1;
        else if (m > 0 && n == 0)
        {
            m–;
            return m.Ackermann(n);
        }
        else
        {
            int temp = m – 1;
            return temp.Ackermann(m.Ackermann(n – 1));
        }
    }

    public static int ReverseNumber(this int number, int r = 0)
    {
        if (number == 0)
            return r;
        else
        {
            int temp = number / 10;
            return temp.ReverseNumber((r * 10) + (number % 10));
        }
    }
}

Advertisements

Responses

  1. Nice post.Visit WPF Extension Methods or visit Extension Method Kit for full set.

    • Thanks to like it. I visited your library and you have some cool methods. Thanks for creating this library.

      Regards
      Zeeshan Amjad

      • Thanks.


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: