❔ Pointer Issue.
My pathetic code:
Terrible error:
Why?
c#
unsafe class Test
{
static void Main()
{
int a = 1; int b = 2;
int* x = &a, y = &b;
Console.WriteLine($"{*x} and {*y}");
Swap(out x, out y);
Console.WriteLine($"{*x} and {*y}");
}
static void Swap(out int* a, out int* b)
{
long x = (long)a;
long y = (long)b;
x ^= y ^= x ^= y;
a = (int*)x; b = (int*)y;
}
}c#
unsafe class Test
{
static void Main()
{
int a = 1; int b = 2;
int* x = &a, y = &b;
Console.WriteLine($"{*x} and {*y}");
Swap(out x, out y);
Console.WriteLine($"{*x} and {*y}");
}
static void Swap(out int* a, out int* b)
{
long x = (long)a;
long y = (long)b;
x ^= y ^= x ^= y;
a = (int*)x; b = (int*)y;
}
}Terrible error:
Error CS0269 Use of unassigned out parameter 'a' ConsoleApp1
Error CS0269 Use of unassigned out parameter 'b' ConsoleApp1 Error CS0269 Use of unassigned out parameter 'a' ConsoleApp1
Error CS0269 Use of unassigned out parameter 'b' ConsoleApp1 Why?