-5

How to swap two variables in C#?

i.e.:

var a = 123;
var b = "hello!";

swap(a, b); // hypothetical

System.Console.WriteLine(a);
System.Console.WriteLine(b);

outputs:

hello!

123

Community
  • 1
  • 1
  • Already asked here: http://stackoverflow.com/questions/804706/swap-two-variables-without-using-a-temp-variable – Milen Jun 07 '13 at 15:43
  • 2
    Not exactly same, there is same type (decimal) – user2160380 Jun 07 '13 at 15:44
  • 2
    You can't. Implicit typing (using var) is still strongly typed. So a is an `int` and b is a `string` and you can't store a `string` in an `int` and vice-versa. – Matt Burland Jun 07 '13 at 15:45
  • 1
    use `object` instead of `var` then you can – cuongle Jun 07 '13 at 15:49
  • 1
    @user2160380 - Yes, you should definitely switch to javaScript. Or VBA. – H H Jun 07 '13 at 15:49
  • I believe it is a great feature. There are different set of functions to work with with different type of variable. It would difficult to debug when we sometimes unsure of what is true identity of your variable. – invisal Jun 07 '13 at 15:56
  • How is this "not constructive"? This question is both real and constructive; perhaps it's a duplicate, but the one linked in a comment misses a twist of a type difference. Nominating for re-opening. – Sergey Kalinichenko Jun 07 '13 at 15:58

3 Answers3

4

You cannot do it with variables of different type. When the variables are of the same type, the simplest way is the "naive" implementation with a temp:

var temp = a;
a = b;
b = temp;

You can wrap this into a swap method that takes its parameters by reference:

static void Swap<T>(ref T a, ref T b) {
    var temp = a;
    a = b;
    b = temp;
}

You cannot change the type of a statically typed variable (var uses a static type determined at compile time). You can declare your variable as object or dynamic if the type of a variable must change at runtime. In case you do it, however, the value types (including ints) will be wrapped in reference types.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You can't do that with var.

If you really needed to do something like that, you could use dynamic and your typical implementation of a swap with temp:

dynamic a = 123;
dynamic b = "hello!";
var temp = a;
a = b;
b = temp;
NominSim
  • 8,447
  • 3
  • 28
  • 38
  • Dynamic could be a bit overkill but it will work definitely. – Dandré Jun 07 '13 at 15:50
  • @dandrejvv Other than declaring them with `object`, `dynamic` is the only way to do what the OP wants. It has the added benefit of not needing to cast whenever you need the specific type. See [this blog post](http://blogs.msdn.com/b/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx) for more info on dynamic vs. object. – NominSim Jun 07 '13 at 15:56
1
object varA = 123;
object varB = "hello!";
object temp = varA;
varA = varB;
varB = temp;

That should work with different types for all .NET versions (I think).

Dandré
  • 2,053
  • 3
  • 18
  • 38