I'm having trouble's with 2 List's in the following code :
internal class Program
{
private static readonly List<char[]> permutations = new List<char[]>();
private static void Main()
{
string str = "0123456789";
char[] arr = str.ToCharArray();
GetPer(arr);
//2. here we have lost all the values
Console.ReadKey();
}
private static void Swap(ref char a, ref char b)
{
if (a == b) return;
a ^= b;
b ^= a;
a ^= b;
}
public static void GetPer(char[] list)
{
int x = list.Length - 1;
GetPer(list, 0, x);
}
private static void GetPer(char[] list, int k, int m)
{
if (k == m)
{
permutations.Add(list); //1. here we add value to the list
}
else
for (int i = k; i <= m; i++)
{
Swap(ref list[k], ref list[i]);
GetPer(list, k + 1, m);
Swap(ref list[k], ref list[i]);
}
}
}
There are 2 comments the first one is in the void GetPer
where we add values to the list. The second comment is in the void Main
where we have lost all the previous values. The code is mostly copy pasted from here Listing all permutations of a string/integer if any explanation is needed. How can I avoid this reference type problem ?