C
C#2y 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
Thinker2y 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
VelcerOP2y ago
How can I easily determine the type in an if statement?
Servator
Servator2y ago
You can use when with case
Anton
Anton2y ago
the is operator
Anton
Anton2y 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
Servator2y 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
phaseshift2y ago
case BaseEntity entity:
entity.Foo()
case BaseEntity entity:
entity.Foo()
etc
Accord
Accord2y 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.
Want results from more Discord servers?
Add your server