0

I have enum:

public enum AlertSubject
    {
        TouristVisa = 1,
        CarDamagene = 2,
        RentalForceClose = 3,
        CarAccident = 4
    }

how to get key like a string? I mean "1" for AlertSubject.TouristVisa

3
  • 5
    ((int)AlertSubject.TouristVisa).ToString(); Commented Jun 25, 2020 at 14:29
  • 2
    Does this answer your question? Get int value from enum in C# Commented Jun 25, 2020 at 14:30
  • Enums in C# are essentially aliases for numbers, which means you can pass them anywhere an int is expected or cast them to an int Commented Jun 25, 2020 at 14:30

1 Answer 1

2

You can use corresponding string format:

var s = AlertSubject.TouristVisa.ToString("d");

Enumeration format strings:

D or d. Displays the enumeration entry as an integer value in the shortest representation possible.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.