Determines whether the Enum flag is on for the specified mask. - CSharp System

CSharp examples for System:Enum

Description

Determines whether the Enum flag is on for the specified mask.

Demo Code


using System.Threading;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Generic;
using System;//from   ww w. j a v  a  2s .  c o m

public class Main{
        /// <summary>
        /// Determines whether the flag is on for the specified mask.
        /// </summary>
        /// <typeparam name="T">The flag type.</typeparam>
        /// <param name="mask">The mask to check if the flag is on.</param>
        /// <param name="flag">The flag to check for in the mask.</param>
        /// <returns>
        ///    <c>true</c> if the flag is on for the specified mask; otherwise, <c>false</c>.
        /// </returns>
        public static bool IsFlagOn<T>(this Enum mask, T flag)
            where T : struct, IComparable, IFormattable, IConvertible {
            ulong flagInt = Convert.ToUInt64(flag);
            ulong maskInt = Convert.ToUInt64(mask);

            return (maskInt & flagInt) == flagInt;
        }
}

Related Tutorials