0

http://msdn.microsoft.com/en-us/library/yd1hzczs.aspx

Specifying the options

The options parameter is a bitwise OR combination of RegexOptions enumerated values.

RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace;

What does this mean? Why is a bitwise operator used and what is the benefit instead of using an array, for example?

shagon
  • 293
  • 3
  • 14
  • 1
    You mean why did they choose to have options specified with a bitwise operator? It's a fairly common way of passing flags to a function, and saves the overhead of passing in an array to a function as the underlying types would be unsigned int/char. Say RegexOptions.IgnoreCase is 1 and RegexOptions.IgnorePatternWhitespace is 2, then the options argument is 1 | 2 = 3. Then you can do `options & RegexOptions.IgnoreCase` to see whether the user wanted that option. It's just faster than arrays. – mathematical.coffee Feb 01 '13 at 02:37
  • They are flags. [Read](http://stackoverflow.com/questions/8447/enum-flags-attribute) – Austin Brunkhorst Feb 01 '13 at 02:38
  • 2
    It is lightweight (implemented with a single integer, rather than an array of boolean). Think of an integer as an array of bit and you will see that you have an array 32 or 64 bools. – nhahtdh Feb 01 '13 at 02:39

1 Answers1

2

This is actually enum flags. Each enum entry is associated with a number and using the bitwise operators you are actually operating on the numbers behind each enum name.

See this for more information: What does the [Flags] Enum Attribute mean in C#?

Community
  • 1
  • 1
Sina Iravanian
  • 16,011
  • 4
  • 34
  • 45
  • I would also look at the [MSDN Article](http://msdn.microsoft.com/en-us/library/kxszd0kx(v=vs.80).aspx) on the `|` operator because it illustrates it's use as both a logical `OR` operator and a bitwise operator. – Leon Newswanger Feb 01 '13 at 02:41