How to swap between two numbers in C#

1 Answer

0 votes
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //example 1
            int a = 3, b = 12;

            Console.WriteLine("a = {0}, b = {1}", a, b);
            swap(ref a, ref b);
            Console.WriteLine("a = {0}, b = {1}", a, b);

            //example 2
            int c = 19, d = 7;

            Console.WriteLine("\nc = {0}, d = {1}", c, d);
            c = c + d;
            d = c - d;
            c = c - d;
            Console.WriteLine("c = {0}, d = {1}", c, d);

        }
        static void swap(ref int x, ref int y)
        {
            int tmp;

            tmp = x;
            x = y;
            y = tmp;
        }
    }
}


answered Apr 8, 2014 by avibootz
...