recursive incremental serial number in csharp

Here's a possible implementation of a recursive function that generates an incremental serial number in C#:

main.cs
public int GenerateSerialNumber(int currentValue, int increment)
{
    // Base case: return the current value if no increment is left
    if (increment <= 0)
        return currentValue;

    // Recursive case: generate the next serial number and call the function again
    int nextValue = currentValue + 1;
    return GenerateSerialNumber(nextValue, increment - 1);
}
370 chars
11 lines

To use this function, you would pass in the current value of the serial number and the number of increments you want to apply. For example, if you want to generate the 5th serial number starting from 100, you would call:

main.cs
int fifthSerialNumber = GenerateSerialNumber(100, 4);
54 chars
2 lines

The 4 is passed in as the increment because we are generating the next serial number 4 times recursively.

gistlibby LogSnag