• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C# » Introduction » What is enumeration in C#
Next →
← Prev

What is enumeration in C#

By Dinesh Thakur

An enumeration is a special set of numeric type string literals which considered as a constants.

An enumeration is a special kind of value type limited to a restricted and unchangeable set of numerical values. By default, these numerical values are integers, but they can also be longs, bytes, etc. (any numerical/value except char) as will be illustrated below.

When you define an enumeration you provide literals which are then used as constants for their corresponding values. The following code shows an example of such a definition:

 

public enum DAYS

{

Monday,

Tuesday,

Wednesday,

Thursday,

Friday,

Saturday,

Sunday

}

 

Note, however, that there are no numerical values specified in the above. Instead, the numerical values are set up according to the following two rules:

 

1. For the first literal: if it is unassigned, set its value to O.

2. For any other literal: if it is unassigned, then set its value to one greater than the value of the preceding literal.

 

From these two rules, it can be seen that DAYS.Monday will be set to 0, and the values increased until DAYS.Sunday is set to 6. Note also how we are referring to these values – the values specified in an enumeration are static, so we have to refer to them in code using the name of the enumeration: “DAYS.Monday” rather than just “Monday”. Furthermore, these values are final-you can’t change their runtime value.

~

The following code demonstrates how you can override the default setting which makes the default values integers. In this example, the enumeration values are set to bytes.

 

enum byteEnum : byte

{

A,

B

}

 

You can also override the default numerical values of any and all of the enumeration elements. In the following example, the first literal is set to value 1. The other literals are then set up according to the second rule given above, so DAYS.Sunday will end up equal to 7.

 

public enum DAYS

{

Monday=!,

Tuesday,

wednesday,

Thursday,

Friday,

saturday,

Sunday,

}

 

In the two examples given, the values of each literal has been’ unique within the enumeration. This is usually how you will want things to be, but in fact the values need not be unique. In the following case, the value of DAYS.Thursday is also set to equal!. The values assigned to the other literals will follow the rules given previously, so both DAYS.Tuesday and DAYS.Friday will equal 2, etc.

public enum DAYS

{

Monday=l,

Tuesday,

wednesday,

Thursday=l,

Friday,

Saturday,

Sunday

}

In C# enumerations are type-safe, by which we mean that the compiler will do its best to stop you assigning implicit values to enumeration typed variables. For instance, the following code should not compile:

 

int i = DAYS.Monday;

DAYS d = i;

In order to get this code to compile, you would have to make explicit casts both ways (even converting from DAYS to int), ie:

 

int i = (int)DAYS.Monday;

DAYS d = (DAYS)i;

At this point you may be wondering what happens if you cast an int to an enumeration value where that same value is defined for two elements within the enumeration. And the answer is this: one of the elements is given ‘primary’ status, so it gets picked ahead of the other.

A useful feature of enumerations is that one can retrieve the literal as a string from the numeric constant with which it· is associated. In fact, this is given by the default To String() method, so the following expression comes out as true:

DAYS.Monday. ToStringO==”Monday”

The following code prints out both the literal and its constant value for the specified enumeration.

 

Example of Enumeration

 

class EnumTest

{

public enum DAys:byte {MON,TUE, WED,THU,FRI,SAT,SUN}

public static void Main(string[] args)

{

DAYS d=EnumTest.DAYS.SAT;

byte a=(byte) EnumTest.DAYS.SAT;

string str=EnumTest. DAYS. SAT.ToString() ;

consol e. writeline(“{O}, {I}, {2}” ,d, d. ToString() ,d. Tostring(“d”)) ;

consol e. writeline(“{O}, {l}”,str, a) ;

}

}

 

OUTPUT:

SAT, SAT, 5

SAT, 5

Since it’s not immediately obvious what’s going on in the main method here, let’s take the time to go through it.

console.Writeline(“{0}, {I}, {2}” ,d, d. ToString() ,d. Tostring(“d”)) ;

And what the String.Format method does is to take ‘textual representations’ of the objects it is passed as parameters, and slots them into the appropriate places within the ‘format string’ it is passed.

Now, we’ve already noted that ToString () will return a literal string, but what about the method d.ToString (“d”)?

The ToString method can take a single IFormatProvider parameter which indicates how the string conversion should be conducted. Values for this parameter can include things like “g”, “d”, “x”, “f’, etc. The stated implication of “d”, however, is to render in ‘Decimal format’.And when we use this on an enumeration member, it provides a string representation of the “numerical value” of the enumeration member.

You’ll also like:

  1. Enumeration Constant in C
  2. Enumeration Data Type
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C# Tutorials

C# Tutorials

  • C# - .NET Languages Types
  • C# - Dot Net
  • C# - Dot Net Framework
  • C# - Inheritance Types
  • C# - Features
  • C# - CTS
  • C# - CLS
  • C# - CLR
  • C# - Console
  • C# - MSIL
  • C# - Base Class Library
  • C# - Web Forms Creation
  • C# - C# Vs C++
  • C# - Statements Types
  • C# - JIT
  • C# - CLI
  • C# - Controls Types
  • C# - String Types
  • C# - Execution Model

Other Links

  • C# - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW