In this article I will explain with an example, how to implement reverse FOR LOOP in C# and VB.Net.
A FOR LOOP iterates from START to END while the reverse FOR LOOP as the name suggests will do REVERSE i.e. from end to start.
This article will explain a simple reverse FOR LOOP by reversing a string in C# and VB.Net.
 
 
Function to Reverse a String using Reverse FOR LOOP in C# and VB.Net
The ReverseString function accepts a string i.e. input as parameter.
First, the input string is converted into an Array of Characters using the ToCharArray function.
Then using a reverse FOR LOOP, the items inside the Character Array are traversed and on by one each character is appended to another string i.e. output.
Thus by appending the character one by one in reverse order, a reverse string is generated.
Finally, the output string is returned.
C#
private static string ReverseString(string input)
{
    string output = string.Empty;
    char[] chars = input.ToCharArray();
    for (int i = chars.Length - 1; i >= 0; i--)
    {
        output += chars[i];
    }
 
    return output;
}
 
VB.Net
Private Shared Function ReverseString(ByVal input As String) As String
    Dim output As String = String.Empty
    Dim chars As Char() = input.ToCharArray()
 
    For i As Integer = chars.Length - 1 To 0 Step -1
        output &= chars(i)
    Next
 
    Return output
End Function
 
 
Downloads