Solutions
Exercise 1
Incorporate the Option
return type to signify that a user may or may not be found:
public Option<Tower> GetTowerByPosition(Vector2 position) { var tower = _gameMap.FindTowerAt(position); return Option<Tower>.Some(tower); }
Exercise 2
Utilize a built-in check for null inputs that will throw an exception if the user profile is null
:
public void ApplyPowerUp(Tower? tower, PowerUp? powerUp) { ArgumentNullException.ThrowIfNull(tower, nameof(tower)); ArgumentNullException.ThrowIfNull(powerUp, nameof(powerUp)); tower.ApplyPowerUp(powerUp); _gameState.UpdateTower(tower); }
Exercise 3
Employ pattern matching to address potential null values elegantly:
public string DescribeEnemy(Enemy? enemy) { return enemy switch ...