What do you think about quality of code in this method?
It should:
- Generate new key based on keys in array
- The key should start with "TG"
- Code should find the max number in array keys with "TG-" and increment this number
- It should have 7 digits
- No need to check what if there is a key TG9999999, and what if array is empty or doesn't have keys "TG-"
private static string[] allKeys = new string[]
{
"TG0000006",
"TG0000026",
"TG0000086",
"TG0000796",
"TG0023106",
"LG0004406",
"MS0000796",
"TT0023106",
"LK0004406",
};
static string GenerateKey()
{
var keys = allKeys.Where(k => k.StartsWith("TG"));
List<int> nums = new List<int>();
foreach (var key in keys.Select(str => Regex.Match(str, @"\d+").Value))
{
int num;
if (int.TryParse(key, out num))
nums.Add(num);
}
string newNum = (nums.Max() + 1).ToString();
return "TG" + new string('0', 7 - newNum.Length) + newNum;
}