Q:

Combination of two characters

I am giving input as int and according to that input I want combination of two characters,
FOR EXAMPLE
 I am giving input as 2 and I have two characters x and y so I want combinations like
    xx,yy,xy,yx

If Input is 3,I want
    xxx,xyy,xxy,xyx,yxx,yyy,yxy.yyx

and so on,I have try with following code,
     int input1 = 4;
        Double totalpossibilities = Math.Pow(2, input1);
        string[] PArray = new string[Convert.ToInt16(totalpossibilities)];
        char[] chars = new char[] { 'x', 'y'};

        for (int i = 0; i < totalpossibilities; i++)
        {
            string possibility = "" ;
            for (int j = 0; j < input1; j++)
            {
                Random random = new Random();
                int r = random.Next(chars.Length);
                char randomChar = chars[r];
                possibility = possibility + randomChar;

            }
            if (PArray.Contains(possibility))
            {
                i--;
            }
            else
                PArray[i] = possibility;
        }

But as you can see I am using random function So I takes too long to complete,Is there any different logic?

A:

Using a copy of the Cartesian Product extension method copied verbatim from here:
static IEnumerable<IEnumerable<T>> CartesianProduct<T>(this IEnumerable<IEnumerable<T>> sequences) 
{ 
  IEnumerable<IEnumerable<T>> emptyProduct = new[] { Enumerable.Empty<T>() }; 
  return sequences.Aggregate( 
    emptyProduct, 
    (accumulator, sequence) => 
      from accseq in accumulator 
      from item in sequence 
      select accseq.Concat(new[] {item})); 
}

Then in your code you can have:
IEnumerable<char> possibleCharacters = "xy";//change to whatever
int numberOfDigits = 3;

var result = Enumerable.Repeat(possibleCharacters, numberOfDigits)
     .CartesianProduct()
     .Select(chars => new string(chars.ToArray()));

//display (or do whatever with) the results
foreach (var item in result)
{
    Console.WriteLine(item);
}

A:

You could run a for loop from 0 to totalpossibilities.
Convert i to binary, for example, at iteration 20 this would result in "10100".
Pad the result to input1 characters, for example (for 8 places): 00010100
Then convert to a string and replace all zeroes with "x", all ones with "y".
        int places = 4;
        Double totalpossibilities = Math.Pow(2, places);

        for (int i = 0; i < totalpossibilities; i++)
        {
            string CurrentNumberBinary = Convert.ToString(i, 2).PadLeft(places, '0');

            CurrentNumberBinary = CurrentNumberBinary.Replace('0', 'x');
            CurrentNumberBinary = CurrentNumberBinary.Replace('1', 'y');
            Debug.WriteLine(CurrentNumberBinary);
        }

