settingsAccountsettings
Menusettings

Q: How to smartly (with programming logic) exchange variable values in C#?

+6 votes

I need to declare two integer variables a and b and assign them with 5 and 10 and after that exchange their values by using some programming logic.

The output must be:

Before:
a = 5
b = 10

After:
a = 10
b = 5

asked in C# category by user eiorgert

1 Answer

+1 vote
using System;

class ExchangeVariableValues
{
    static void Main()
    {
        int a = 5;
        int b = 10;
        Console.WriteLine("Before:\na={0}\nb={1}\n",a,b);

        int c = a;
        a = b;
        b = c;
        Console.WriteLine("After:\na={0}\nb={1}\n",a,b);
    }
}

 

answered by user paulcabalit
edited by user golearnweb
...