rspec/rules/S3265/csharp/rule.adoc

44 lines
953 B
Plaintext
Raw Normal View History

2021-04-28 16:49:39 +02:00
``++enum++``s are usually used to identify distinct elements in a set of values. However ``++enum++``s can be treated as bit fields and bitwise operations can be used on them to combine the values. This is a good way of specifying multiple elements of set with a single value. When ``++enum++``s are used this way, it is a best practice to mark the ``++enum++`` with the ``++FlagsAttribute++``.
2021-04-28 16:49:39 +02:00
== Noncompliant Code Example
----
enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Execute = 4
}
// ...
var x = Permissions.Read | Permissions.Write; // Noncompliant; enum is not marked with [Flags]
----
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
[Flags]
enum Permissions
{
None = 0,
Read = 1,
Write = 2,
Execute = 4
}
// ...
var x = Permissions.Read | Permissions.Write;
----
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]