C# enum


// ---------------------- HOW TO USE ENUMS? ----------------------- //

// How to create?

public enum Colors   // It needs to be defined at the namespace level (outside any class)! 
{
	red = 1,
    green = 2,
    blue = 3,
    white = 4,
    black = 5

}

// How to get the values?

var itemRed = Colors.red;

Console.WriteLine((int)itemRed);  // Using casting to convert to int


// How to get the keys?

var itemX = 4;

Console.WriteLine((Colors)itemX);  // Using casting to convert to Colors
 

// How to convert enums to strings? 

var itemBlue = Colors.blue;

Console.WriteLine(itemBlue.ToString());


// How to convert strings to enums? 

var colorName = "green";

var enumName = (Colors)Enum.Parse(typeof(Colors), colorName);

Console.WriteLine(enumName);       // To see the key
Console.WriteLine((int)enumName);  // To see the value
 



4.44
9

                                    enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

4.44 (9 Votes)
0
0
7
Anggie 85 points

                                    enum Level 
{
  Low,
  Medium,
  High
}

Level myVar = Level.Medium;
Console.WriteLine(myVar);

0
0
3.57
7
Lizzielap 100 points

                                    enum Level 
{
  Low,
  Medium,
  High
}

3.57 (7 Votes)
0
3.63
8
Cnread 105 points

                                    enum Season
{
    Spring,
    Summer,
    Autumn,
    Winter
}

3.63 (8 Votes)
0
4.14
7

                                    enum CellphoneBrand { 
        Samsung,
        Apple,
  		LG,
  		Nokia,
  		Huawei,
  		Motorola
    }

4.14 (7 Votes)
0
4.33
6
Llaves 75 points

                                    enum Level 
{
  Low,
  Medium,
  High
}

//You can access enum items with the dot syntax:
Level myVar = Level.Medium;
Console.WriteLine(myVar);

4.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
c# enum class example c# enum function using enum c# c# using enum enum c# definition enum example in c# c # enum in using enum c# c# enum in a class c# enum definition c# enum in model c# enum int enum em c# c# enums enums em c# c# enum object c# enum model c# enum example enum csharp c# enum | operation how to use enum in c# class enum class c# what is enum in c# with example c# enum methods enum example c# enum in c# example c sharp enums c sharp enum f# enum enums c# Use Enum Example C# c# enum keyword using enums in c# C# enums examples c# byte enum c# reference enum in class enum in asp.net c# enum in c sharp integer to enum object in C# csharp enum enum parameter c# example c# where do i declare an enum enuim example in solidi using enum in c# enumerate function in c# enum to int c# byte type in enum c# access enum c# add to enum c# how to initialize enum in csharp enum class example c# Enum c# declaration c# enum variable enumerated types C# c sharp enumeration how to write a enum as a variable in c# creating an enumeration in c# declare enum c# make enum as object in c# how to reference enum in c# enum class in c# enum default type int from enum c# c# enums with values what are enums in c# enums in c# what is the role of the | operator on C# enums c# enumarator int how to change variable type of enum in c# enum default typec# Enum c sharp enumeration c# enum c# example How to make an enumeration of enumerations in c# How to make an enum of enums in C# full guide to enums c# enumerators examples c sharp enum c# visual studio using enum for login c# C# enum class enum with associated value C# enum default c# enum variable c# c# enum to int what is enumeration in c# using enum in .net enum syntax c# class constructor takes only first enum element c# c# enum example how to use public enum in c# set type to an enum c# use enums in c# how to add enum in c# making an enumerator for music catagorys c# what types can be assigned to enum c# enumeration in c# what are enums c# how to write enums in c# public enum starts c# c# create enum class C# using public enums enum structure in c#. c# declare enum different values c# udon enums C sharp definging an enum enum(3) c# c# class enum example enumurator .net core reference an enum c# enumerations c sharp c# waht sre enum how to set enum c# using custom enums C# .net enum example c# enumerate c# enum of types c# using enums c# declare enum how to declare an enum in c# c# what are enums used for enum member c# c# enumeration C# enum
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source