ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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

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

Here, we will learn about the GetValues() method of Enum class. This method is used to Retrieves an array of the values of the constants in the specified enum.

Syntax:

    Array Enum.GetValues(Type enumType);

Parameter:

Here we pass an instance of a specified enum.

Return value:

This method returns an array of the values of the constants in the specified enum.

Exceptions:

  • System.ArgumentException
  • System.ArgumentNullException

Program:

The source code to demonstrate the GetValues() 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()
    {
        Console.WriteLine("Values of Directions:");
        foreach (var val in Enum.GetValues(typeof(Directions)))
        {
            Console.WriteLine("{0,3}     0x{0:X8}     {1}",(int)val, ((Directions)val));
        }   
    }
}

Output:

Values of Directions:
  0     0x00000000     EAST
  1     0x00000001     WEST
  2     0x00000002     NORTH
  3     0x00000003     SOUTH
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.