-2

In the following code block there are a few bitwise OR's. I've never used them before so I was trying to understand what the code block meant.

    Document doc = new Document("CleanupOptions.docx");
    doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions |
    MailMergeCleanupOptions.RemoveUnusedFields |
    MailMergeCleanupOptions.RemoveContainingFields;
    doc.MailMerge.ExecuteWithRegions(dataSet);

So in that block above, if I used doc.MailMerge.CleanupOptions, how would I pick any of the statements that the CleanupOptions are equal to? Or are they all combined?

Bmoe
  • 888
  • 1
  • 15
  • 37

2 Answers2

0

They are all combined. An enum can be marked with the [FlagsAttribute] which allows combination of values:

https://msdn.microsoft.com/en-us/library/system.flagsattribute(v=vs.110).aspx

nodots
  • 1,450
  • 11
  • 19
0

The MailMergeCleanupOptions is an enum with the FlagsAttribute specified. This allows you to do bit-wise ors to join the values into a collection. Normally the values are powers of two or a combination of flags.

John Taylor
  • 446
  • 3
  • 8
  • It's not really the `[Flags]` attribute doing that though, that attribute does very little. It's the enum itself combined with how it is interpreted by the consuming code. – harold Apr 01 '15 at 13:52
  • The compiler will complain if you do a bitwise or on an enum that does not have the [Flags] attribute. – John Taylor Apr 01 '15 at 14:26
  • No it doesn't, you even made me doubt myself enough that I went to try it – harold Apr 01 '15 at 14:41
  • Must be ReSharper that complains. Sometimes I lose track which one is generating which warnings. – John Taylor Apr 01 '15 at 14:45