Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I based this method off of a bunch of SO &amp; Google searches, and a by using reflector to see what MS did for the .NET 4 HasFlags method.</p> <pre><code>public static class EnumExt { /// &lt;summary&gt; /// Check to see if a flags enumeration has a specific flag set. /// &lt;/summary&gt; /// &lt;param name="variable"&gt;Flags enumeration to check&lt;/param&gt; /// &lt;param name="value"&gt;Flag to check for&lt;/param&gt; /// &lt;returns&gt;&lt;/returns&gt; public static bool HasFlag(this Enum variable, Enum value) { if (variable == null) return false; if (value == null) throw new ArgumentNullException("value"); // Not as good as the .NET 4 version of this function, but should be good enough if (!Enum.IsDefined(variable.GetType(), value)) { throw new ArgumentException(string.Format( "Enumeration type mismatch. The flag is of type '{0}', was expecting '{1}'.", value.GetType(), variable.GetType())); } ulong num = Convert.ToUInt64(value); return ((Convert.ToUInt64(variable) &amp; num) == num); } } </code></pre> <p>Notes:</p> <ul> <li>This handles nulls</li> <li>Does type checking</li> <li>Converts to a ulong, and can handle any positive enum value. <a href="http://msdn.microsoft.com/en-us/library/system.enum.aspx" rel="noreferrer">Microsoft cautions</a> against the use of negative flags enumerations anyway: <blockquote> <p>Use caution if you define a negative number as a flag enumerated constant because many flag positions might be set to 1, which might make your code confusing and encourage coding errors.</p> </blockquote></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload