Hello, I'm a beginner in these to programming language. Here I want to know how to display the actual value of variable of enum holds. Like this in C++:

#include <iostream>
#include <conio.h>
using namespace std;

int main(void)
{
    enum Difficulty
    {
         Low = 1,
         Medium,
         High
    };
    
    Difficulty myDiff = High;
    
    cout << "Difficulty: " << myDiff << endl; //it displays the value is 3
    
    _getch();
}[/B]

but in C#:

using System;

public enum Difficulty : byte
{
    Low = 1,
    Medium,
    High
}

class enumProgram
{
    public static void Main(string[] args)
    {
        Difficulty myDiff = Difficulty.High;

        Console.WriteLine("High = {0}", myDiff); //it displays High instead of 3

        Console.ReadLine();
    }
}

How to display the value 3 instead of High in C#?

Recommended Answers

All 2 Replies

Welcome to the Daniweb.

Please use BB code tags while posting source program.

Use (cast) operator.

Console.WriteLine("High = {0} {1}", myDiff, (int)myDiff );

This is one of the reasons I like C# so much.
An enumeration in C# is what it says: an enumeration, not an integer.
But if you like you can still dig down below the surface.
Same holds for booleans in C++d you can say:

while(0)
{ 
// do something forever
}

This is not allowed in C#, you would have to use a boolean expression.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.