0

I have got method:

private bool MyMethod(PlantType plantType)
{
    return plantType.PlantMoveType == PlantMoveType.PlantReady 
           || plantType.PlantMoveType == PlantMoveType.PlantRelase
}

Can I write it into other way? Maybe with LINQ?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
4est
  • 3,010
  • 6
  • 41
  • 63
  • 1
    What's wrong with the current way? Do you want to shorten it or something? – Sweeper Sep 18 '19 at 06:17
  • What other way do you mean? the only other thing i can think of is `private bool MyMethod(PlantType plantType) => new [] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase}.Contains(plantType.PlantMoveType);` – zaitsman Sep 18 '19 at 06:18
  • as example LINQ is so popular now I want to know how can I use it here – 4est Sep 18 '19 at 06:29

2 Answers2

2

One way is to put the enum values that you want to check against into an array, and call Contains.

return new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
                 .Contains(plantType.PlantMoveType);

If you are using C# 7 or later, you can also write the method as expression-bodied:

private bool MyMethod(PlantType plantType) =>
    new[] { PlantMoveType.PlantReady, PlantMoveType.PlantRelase }
        .Contains(plantType.PlantMoveType);
Kjartan
  • 18,591
  • 15
  • 71
  • 96
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0

Well a small simplification would be to pass the type (enum?) of the property PlantMoveType instead of PlantType as the parameter.

Beyond that, you could declare the types to check for as e.g. an array. In case you'd like to reuse that array, you can also declare it outside the scope of the method:

private static PlantMoveType[] _plantStates = 
   new []{PlantMoveType.PlantReady, PlantMoveType.PlantRelase};

private bool MyMethod(PlantMoveType plantMoveType)
{
    return _plantStates.Contains(plantMoveType);
}
Kjartan
  • 18,591
  • 15
  • 71
  • 96