mercredi 29 janvier 2020

Optimize multiple if conditions for flag check

[Flags]
public enum PatternTypes
{
  A = 1,
  B = 2,
  C = 4,
  D = 8,
  E = 16,
  F = 32,
  G = 64
}

patternsToFind [this is PatternTypes]: it is contain flags value to find number of pattern like A | B | E that will return us A, B and E pattern result in list

if patternsToFind.HasFlag(A) {find pattern A and add to list result}
if patternsToFind.HasFlag(B) {find pattern B and add to list result}
if patternsToFind.HasFlag(C) {find pattern C and add to list result}
if patternsToFind.HasFlag(D) {find pattern D and add to list result}
if patternsToFind.HasFlag(E) {find pattern E and add to list result}

like this, I have 20 pattern so what should i use to optimize it? Alternate to multiple if conditions. Either i have to write it 20 times.

Aucun commentaire:

Enregistrer un commentaire