×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

C# - Enum.HasFlag() Method with Example

In this tutorial, we will learn about the C# Enum.HasFlag() method with its definition, usage, syntax, and example. By Nidhi Last updated : March 29, 2023

C# Enum.HasFlag() Method

The Enum.HasFlag() method is used to determine one or more bit fields are set in the current instance.

Syntax

bool Enum.HasFlag(Enum flag);

Parameter(s)

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(s)

  • System.ArgumentException;

C# Example of Enum.HasFlag() Method

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!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.