Created
October 29, 2021 11:28
-
-
Save MichalBrylka/815380b1ef690483f91356b6b6bca8de to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace IntSwap | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
int i = 1, j = 2; | |
Swap1(ref i, ref j); | |
Console.WriteLine($"i = {i}, j = {j}"); | |
Swap2(ref i, ref j); | |
Console.WriteLine($"i = {i}, j = {j}"); | |
int? nullI = 1, nullJ = 2; | |
Swap3(nullI, nullJ); | |
Console.WriteLine($" nullI = { nullI }, nullJ = { nullJ }"); | |
/*OUTPUT: | |
i = 2, j = 1 | |
i = 1, j = 2 | |
nullI = 1, nullJ = 2*/ | |
} | |
public static void Swap1(ref int a, ref int b) | |
{ | |
var temp = a; | |
a = b; | |
b = temp; | |
} | |
public static void Swap2(ref int a, ref int b) => (a, b) = (b, a); | |
public static void Swap3(int? a, int? b) | |
{ | |
var temp = a; | |
a = b; | |
b = temp; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment