C
C#17mo ago
Velcer

❔ Continue a switch statement

The switch statement is very convenient for managing my types. I would like to instead of just having one of these hit, for it to hit any that meet criteria.
switch (kinematic)
{
case PlayerShip playerShip:
playerShipsEnteredProximityOfEntity.Add(new KeyValuePair<ushort, ushort> (kinematic.KinematicId, collision.Id));
break;
case BaseEntity entity:
if (!entity.EntitiesInProximity.Contains(collision.Id))
entity.EntitiesInProximity.Add(collision.Id);
break;
}
switch (kinematic)
{
case PlayerShip playerShip:
playerShipsEnteredProximityOfEntity.Add(new KeyValuePair<ushort, ushort> (kinematic.KinematicId, collision.Id));
break;
case BaseEntity entity:
if (!entity.EntitiesInProximity.Contains(collision.Id))
entity.EntitiesInProximity.Add(collision.Id);
break;
}
8 Replies
Thinker
Thinker17mo ago
You can let a case fall through to the next case, but I don't think that's what you want. I think a simple series of If-statements would be the simplest here.
Velcer
Velcer17mo ago
How can I easily determine the type in an if statement?
Servator
Servator17mo ago
You can use when with case
Anton
Anton17mo ago
the is operator
Anton
Anton17mo ago
The is operator - Match an expression against a type or constant ...
Learn about the C# is operator that matches an expression against a pattern. The is operator returns true when the expression matches the pattern.
Servator
Servator17mo ago
:S
case BaseEntity entity when !entity.EntitiesInProximity.Contains(collision.Id):
// bla bla
break;
case BaseEntity entity when !entity.EntitiesInProximity.Contains(collision.Id):
// bla bla
break;
phaseshift
phaseshift17mo ago
case BaseEntity entity:
entity.Foo()
case BaseEntity entity:
entity.Foo()
etc
Accord
Accord17mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.