While the solution linked to here by Philippe Mori based (probably) on the suggestion by Alan N. in his comment will, indeed, get the correct result, do you really need to make the solution this complex ?
Why not simply first generate six random letters, and, then, you can easily sort them, and then generate two random letters ?
private static Random randGenerator = new Random(DateTime.Now.Millisecond);
private static string Letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public IEnumerable<char> GetRandomChars(int count, bool useuppercase = false)
{
for (int i = 0; i < count; i++)
{
yield return
(useuppercase)
? Letters[randGenerator.Next(0, 52)]
: Letters[randGenerator.Next(0, 26)];
}
}
// use example: execute in some Method or EventHandler
var firstsix = GetRandomChars(6, false).ToList();
firstsix.Sort();
var lasttwo = GetRandomChars(2, false);
var result = firstsix.Concat(lasttwo);
Note:
1. this code goes back many years to pre-Linq days, and was updated, after the first version of Linq came along, to return an IEnumerable result for convenience.
2. note that you have to "evaluate to an actual result" the IEnumerable contained in the var 'firstsix before you can call the 'Sort operator, and note that 'Sort transforms its argument but returns nothing (i.e., 'void).
3. The final var 'result will contain an IEnumerable<char>, so keep in mind that "deferred evaluation" is in effect here; that's something I recently tried to explain here: [
^].
4. note the 'Sort operator used here is not Linq; it's a method provided by System.Collections.Generic, which arrived with C#2.0