ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

C# program to demonstrate the HasFlag() method of Enum class

Here, we are going to demonstrate the HasFlag() method of Enum class in C#.Net.
Submitted by Nidhi, on April 17, 2021

Here, we will learn about the HasFlag() method of Enum class. This method is used to determine one or more bit fields are set in the current instance.

Syntax:

    bool Enum.HasFlag(Enum flag);

Parameter:

Here we pass the flag of a created enum.

Return value:

This method returns the boolean value if the bit field is set then it returns true otherwise it returns false.

Exception:

  • System.ArgumentException;

Program:

The source code to demonstrate the HasFlag() method of Enum class is given below. The given program is compiled and executed successfully.

using System;

class Sample
{
    enum Directions { EAST=0,WEST=1,NORTH=2,SOUTH=3};
   
    //Entry point of Program
    static public void Main()
    {
        Directions[] direction = { Directions.WEST,Directions.SOUTH,Directions.EAST };

        //EAST   0  00
        //WEST   1  01
        //NORTH  2  10
        //SOUTH  3  11

        foreach (Directions dir in direction)
        {
            Console.WriteLine("High bits:");
            if (dir.HasFlag(Directions.WEST))
                Console.WriteLine("\tWEST");

            if (dir.HasFlag(Directions.EAST))
                Console.WriteLine("\tEAST");
        }
    }
}

Output:

High bits:
        WEST
        EAST
High bits:
        WEST
        EAST
High bits:
        EAST
Press any key to continue . . .

C# Enum Class Programs »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.