Swap two variables without using a temporary varia

2019-01-02 18:15发布

I'd like to be able to swap two variables without the use of a temporary variable in C#. Can this be done?

decimal startAngle = Convert.ToDecimal(159.9);
decimal stopAngle = Convert.ToDecimal(355.87);

// Swap each:
//   startAngle becomes: 355.87
//   stopAngle becomes: 159.9

28条回答
深知你不懂我心
2楼-- · 2019-01-02 18:27

If you want to swap 2 string variables:

a = (a+b).Substring((b=a).Length);

An helper method accordingly:

public static class Foo {
    public static void SwapString (ref string a, ref string b) {
       a = (a+b).Substring((b=a).Length);
    }
}

Usage would be then:

string a="Test 1";
string b="Test 2";
Foo.SwapString(a, b);
查看更多
几人难应
3楼-- · 2019-01-02 18:28

For the sake of future learners, and humanity, I submit this correction to the currently selected answer.

If you want to avoid using temp variables, there are only two sensible options that take first performance and then readability into consideration.

  • Use a temp variable in a generic Swap method. (Absolute best performance, next to inline temp variable)
  • Use Interlocked.Exchange. (5.9 times slower on my machine, but this is your only option if multiple threads will be swapping these variables simultaneously.)

Things you should never do:

  • Never use floating point arithmetic. (slow, rounding and overflow errors, hard to understand)
  • Never use non-primitive arithmetic. (slow, overflow errors, hard to understand) Decimal is not a CPU primitive and results in far more code than you realize.
  • Never use arithmetic period. Or bit hacks. (slow, hard to understand) That's the compiler's job. It can optimize for many different platforms.

Because everyone loves hard numbers, here's a program that compares your options. Run it in release mode from outside Visual Studio so that Swap is inlined. Results on my machine (Windows 7 64-bit i5-3470):

Inline:      00:00:00.7351931
Call:        00:00:00.7483503
Interlocked: 00:00:04.4076651

Code:

class Program
{
    static void Swap<T>(ref T obj1, ref T obj2)
    {
        var temp = obj1;
        obj1 = obj2;
        obj2 = temp;
    }

    static void Main(string[] args)
    {
        var a = new object();
        var b = new object();

        var s = new Stopwatch();

        Swap(ref a, ref b); // JIT the swap method outside the stopwatch

        s.Restart();
        for (var i = 0; i < 500000000; i++)
        {
            var temp = a;
            a = b;
            b = temp;
        }
        s.Stop();
        Console.WriteLine("Inline temp: " + s.Elapsed);


        s.Restart();
        for (var i = 0; i < 500000000; i++)
        {
            Swap(ref a, ref b);
        }
        s.Stop();
        Console.WriteLine("Call:        " + s.Elapsed);

        s.Restart();
        for (var i = 0; i < 500000000; i++)
        {
            b = Interlocked.Exchange(ref a, b);
        }
        s.Stop();
        Console.WriteLine("Interlocked: " + s.Elapsed);

        Console.ReadKey();
    }
}
查看更多
十年一品温如言
4楼-- · 2019-01-02 18:28

With tuples

decimal startAngle = Convert.ToDecimal(159.9);
decimal stopAngle = Convert.ToDecimal(355.87);

(startAngle, stopAngle) = (stopAngle, startAngle);
查看更多
呛了眼睛熬了心
5楼-- · 2019-01-02 18:30

Here is some different process to swap two variables

//process one
a=b+a;
b=a-b;
a=a-b;
printf("a= %d  b=  %d",a,b);

//process two
a=5;
b=10;
a=a+b-(b=a);
printf("\na= %d  b=  %d",a,b);

//process three
a=5;
b=10;
a=a^b;
b=a^b;
a=b^a;
printf("\na= %d  b=  %d",a,b);

//process four
a=5;
b=10;
a=b-~a-1;
b=a+~b+1;
a=a+~b+1;
printf("\na= %d  b=  %d",a,b);
查看更多
孤独总比滥情好
6楼-- · 2019-01-02 18:33
int a = 4, b = 6;
a ^= b ^= a ^= b;

Works for all types including strings and floats.

查看更多
春风洒进眼中
7楼-- · 2019-01-02 18:34
var a = 15;
var b = -214;
a = b | !(b = a);

This works great.

查看更多
登录 后发表回答