You need to specify the same cast when you do the assignment. The compiler does not carry information through the
if statement once the initial check has passed.
In your code:
if (armor is HeadArmor) // The compiler verifies this is HeadArmor
// The compiler does not carry the information forward.
// It has no way of knowing, on this line, that armor is still HeadArmor.
// For all it knows, armor could be ChestArmor which would be invalid.
HeadArmor = armor;
To fix the error, you need to update your code as follows:
if (armor is HeadArmor)
HeadArmor = (HeadArmor)armor; // Lets the compiler know this is HeadArmor.
Another option could be to use
as to try and suss out the correct choice.
var headArmor = armor as HeadArmor;
if (headArmor != null)
HeadArmor = headArmor;