Skip to content

Instantly share code, notes, and snippets.

@hyrmn
Last active Oct 10, 2021
Embed
What would you like to do?
Ways to swap numbers (.NET 6.0)
using System.Diagnostics;
int x = 10;
int y = 20;
var tmp = x;
x = y;
y = tmp;
Debug.Assert(x == 20);
Debug.Assert(y == 10);
using System.Diagnostics;
int x = 10;
int y = 20;
x = x + y;
y = x - y;
x = x - y;
Debug.Assert(x == 20);
Debug.Assert(y == 10);
//Don't do this
using System.Diagnostics;
int x = 10;
int y = 20;
x = x ^ y;
y = x ^ y;
x = x ^ y;
Debug.Assert(x == 20);
Debug.Assert(y == 10);
using System.Diagnostics;
int x = 10;
int y = 20;
int[] scratch = new int[2];
scratch[0] = x;
scratch[1] = y;
x = scratch[1];
y = scratch[0];
Debug.Assert(x == 20);
Debug.Assert(y == 10);
using System.Diagnostics;
int x = 10;
int y = 20;
Swap(ref x, ref y);
Debug.Assert(x == 20);
Debug.Assert(y == 10);
void Swap(ref int x, ref int y)
{
x = x + y;
y = x - y;
x = x - y;
}
//using obvious domain language and value based objects (int is a value) always leads to cleaner understanding
using System.Diagnostics;
int x = 10;
int y = 20;
var swappedIntegers = Swap(x, y);
Debug.Assert(swappedIntegers.newX == 20);
Debug.Assert(swappedIntegers.newY == 10);
(int newX, int newY) Swap(int x, int y) => (y, x);
using System;
using System.Diagnostics;
var xAndY = new[] { 10, 20 };
int? x = null;
int? y = null;
var chooser = new Random();
while(x is null || y is null)
{
//will choose 0 or 1
var choice = chooser.Next(2);
if (xAndY[choice] == 20) x = xAndY[choice];
if (xAndY[choice] == 10) y = xAndY[choice];
}
Debug.Assert(x == 20);
Debug.Assert(y == 10);
//Courtesy of https://twitter.com/augustoproiete
using System;
using System.Diagnostics;
using System.IO;
int x = 10;
int y = 20;
Swap(ref x, ref y);
Debug.Assert(x == 20);
Debug.Assert(y == 10);
static void Swap(ref int x, ref int y)
{
var pathX = Path.GetTempFileName();
var pathY = Path.GetTempFileName();
File.WriteAllBytes(pathX, BitConverter.GetBytes(x));
File.WriteAllBytes(pathY, BitConverter.GetBytes(y));
x = BitConverter.ToInt32(File.ReadAllBytes(pathY));
y = BitConverter.ToInt32(File.ReadAllBytes(pathX));
File.Delete(pathX);
File.Delete(pathY);
}
//Courtesy of https://twitter.com/ylorph
using System.Diagnostics;
using System.Threading;
int x = 10;
int y = 20;
y = Interlocked.Exchange(ref x, y);
Debug.Assert(x == 20);
Debug.Assert(y == 10);
using System;
using System.Diagnostics;
using System.Linq;
int x = 10;
int y = 20;
var sequence = Enumerable.Range(Math.Min(x, y), Math.Max(x, y) - Math.Min(x, y) + 1);
x = sequence.Max();
y = sequence.Min();
Debug.Assert(x == 20);
Debug.Assert(y == 10);
//You will need to first call `dotnet add package LiteDB`
using LiteDB;
using System.Diagnostics;
using System.IO;
int x = 10;
int y = 20;
using var db = new LiteDatabase(new MemoryStream());
var col = db.GetCollection<Value>(nameof(Value));
col.Upsert(new Value(x));
col.Upsert(new Value(y));
var valY = col.FindOne(v => v.val == 10);
var valX = col.FindOne(v => v.val == 20);
Debug.Assert(valX.val == 20);
Debug.Assert(valY.val == 10);
record Value(int val);
@maartenba

This comment has been minimized.

Copy link

@maartenba maartenba commented Sep 9, 2021

10_ThereIsMore

using System;
using System.Diagnostics;

int x = 10;
int y = 20;

(x, y) = (y, x);

Debug.Assert(x == 20);
Debug.Assert(y == 10);

11_FuckItImDone

using System.Diagnostics;

Debug.Assert(true);
@augustoproiete

This comment has been minimized.

Copy link

@augustoproiete augustoproiete commented Sep 9, 2021

12_LINQIsBeautiful.cs

using System.Linq;

int x = 10;
int y = 20;

var numbers = new [] { x, y };
x = numbers.Last();
y = numbers.First();

Debug.Assert(x == 20);
Debug.Assert(y == 10);

https://dotnetfiddle.net/6Eeo9W


13_LINQIsBeautifulAndVersatile.cs

using System.Linq;

int x = 10;
int y = 20;

var numbers = new [] { x, y };
x = numbers.Skip(1).First();
y = numbers.First();

Debug.Assert(x == 20);
Debug.Assert(y == 10);

https://dotnetfiddle.net/O0OPz7

@ylorph

This comment has been minimized.

Copy link

@ylorph ylorph commented Sep 10, 2021

14_Did_I_Really_Undertand_The_Question.cs

int x= 10; 
int y= 20;
x= 20;
y=10;


Debug.Assert(x == 20);
Debug.Assert(y == 10);
@aarondandy

This comment has been minimized.

Copy link

@aarondandy aarondandy commented Sep 10, 2021

15_Deconswap

int x = 10;
int y = 20;
        
(x, y) = new Swap<int>(x, y);
		
Debug.Assert(x == 20);
Debug.Assert(y == 10);
// ...
record Swap<T>(T X, T Y) { public void Deconstruct(out T x, out T y) => (x, y) = (Y, X); }
@aarondandy

This comment has been minimized.

Copy link

@aarondandy aarondandy commented Sep 10, 2021

16_Spoopy_Swap

int x = 10;
int y = 20;																																												(x,y)=(y,x);

		

Debug.Assert(x == 20);
Debug.Assert(y == 10);

https://dotnetfiddle.net/ItrkEs

@augustoproiete

This comment has been minimized.

Copy link

@augustoproiete augustoproiete commented Sep 10, 2021

17_OperatorOverlordsOfTheUniverse.cs

Int x = 10;
Int y = 20;

Int.Swap(x, y);

Debug.Assert(x == 20);
Debug.Assert(y == 10);

public class Int
{
    private int _value;

    public static void Swap(Int x, Int y)
    {
        var tmp = x._value;
        x._value = y._value;
        y._value = tmp;
    }

    public static implicit operator Int (int value)
    {
        return new Int { _value = value };
    }

    public static implicit operator int(Int value)
    {
        return value._value;
    }
}

https://dotnetfiddle.net/97CYLb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment