From a functional perspective, you could change your `ProcessCharArray` to stop allocating unneeded memory on the heap by using this. Being that a `char` is derivative of `ValueType`. The memory will be allocated on the stack within your defined scope and will keep the GC from cleaning up the mess that's created with `characterInString.ToString().ToUpperInvariant() == "X"`.
public static int ProcessCharArray(char characterInString, Frame Frames, int
stratingPinsNumber)
{
int pinsCount = 0;
if (char.IsDigit(characterInString))
{
Frames.Throws.Add((int)char.GetNumericValue(characterInString));
pinsCount = int.Parse(characterInString.ToString());
}
else if (char.ToUpperInvariant(characterInString) == 'X')
{
Frames.IsStrike = true;
Frames.IsFrameOver = true;
Frames.Throws.Add(10);
pinsCount = 10;
}
else if (characterInString == '/')
{
Frames.IsSpare = true;
Frames.IsFrameOver = true;
Frames.Throws.Add(stratingPinsNumber - Frames.Throws[0]);
pinsCount = 10;
}
else if (characterInString == '-')
{
Frames.Throws.Add(0);
pinsCount += 0;
}
else if (characterInString == '/' && Frames.IsLastFrame && Frames.IsBonusAllowed)
{
throw new ArgumentException("The Spare cannot be set on the Bonus Throws, please check.");
}
else
{
throw new ArgumentException($"Invalid argument '{characterInString}' was detected in the provided input, please check.");
}
return pinsCount;
}
here is an alternative approach for the logic in your method as well. :)
if (char.IsDigit(characterInString))
{
Frames.Throws.Add((int)char.GetNumericValue(characterInString));
var pinsCount = int.Parse(characterInString.ToString());
return pinsCount;
}
switch(char.ToUpperInvariant(characterInString))
{
case 'X':
Frames.IsStrike = true;
Frames.IsFrameOver = true;
Frames.Throws.Add(10);
return 10;
case '-':
Frames.Throws.Add(0);
return 0;
case '/':
if(Frames.IsLastFrame && Frames.IsBonusAllowed)
throw new ArgumentException("The Spare cannot be set on the Bonus Throws, please check.");
Frames.IsSpare = true;
Frames.IsFrameOver = true;
Frames.Throws.Add(stratingPinsNumber - Frames.Throws[0]);
return 10;
default : throw new ArgumentException($"Invalid argument '{characterInString}' was detected in the provided input, please check.");
}