C# C中的|是什么?

C# C中的|是什么?,c#,bitwise-operators,aspose.words,bitwise-or,C#,Bitwise Operators,Aspose.words,Bitwise Or,在下面的代码块中有一些按位OR。我以前从未使用过它们,所以我试图理解代码块的含义 Document doc = new Document("CleanupOptions.docx"); doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveUnusedFields | MailMergeCleanupOpt

在下面的代码块中有一些按位OR。我以前从未使用过它们,所以我试图理解代码块的含义

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

因此,在上面的那个块中,如果我使用doc.MailMerge.cleanuptoptions,我将如何选择cleanuptoptions等于的任何语句?或者它们都是组合在一起的?

它们都是组合在一起的。可以使用[FlagsAttribute]标记枚举,该属性允许组合值:


MailMergeCleanupOptions是指定了FlagsAttribute的枚举。这允许您执行逐位OR操作,将值连接到集合中。通常,值是两个或两个标志组合的幂。

这个Q/a对[flags]枚举有很好的解释:它假设CleanupOptions是一个位域枚举,这只是包含常量……但实际上[flags]属性并没有这样做,该属性做的很少。这是枚举本身以及消费代码对它的解释方式。如果您对没有[Flags]属性的枚举执行按位or操作,编译器会抱怨。不,没有。您甚至让我怀疑自己,以至于我去尝试了它,但抱怨的肯定是ReSharper。有时我会忘记哪一个正在生成哪些警告。