-2

When I add this code

DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowInnerWhite

Intellisense shows the following message:

DateTimeStyles DateTimeStyles.operator |(DateTimeStyles left, DateTimeStyles right).

What does this operator do?

musefan
  • 47,875
  • 21
  • 135
  • 185
Shivku
  • 156
  • 3
  • 10

2 Answers2

3

DateTimeStyles isn't an operator - it's an enum, and all enums have the | operator. All it does is apply a bitwise | for the two values. It should only be used for flag-based enums. For example:

public enum AccessMode
{
    None = 0,
    Read = 1,
    Write = 2,
    Delete = 4
}

If you use:

AccessMode mode = AccessMode.Write | AccessMode.Delete;

then you'll have a value with an underlying integer value of 6.

Basically, it allows you to specify a single value representing multiple flags within the enum - so for you're example, you're saying "I want the result to be adjusted to UTC, and allow inner whitespace when parsing."

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
3

DateTimeStyles is a Flags enumeration. The BITWISE OR operator | combines two flags.

See Enum, Flags and bitwise operators.

Georg Patscheider
  • 9,357
  • 1
  • 26
  • 36