SlideShare a Scribd company logo
C# ENUMERATIONS
ENGR. MICHEAL OGUNDERO
You can contact me via
PHONE - +2348087365099
SKYPE – OGUNDERO MICHEAL
Email – ogunderoayodeji@gmail.com
A QUICK ONE!
• Write a program that reads an input (integer), day from a user and
prints to the console based on the if conditional statements below:
If day is 1, print “Monday”
If day is 2, print “Tuesday”
.
.
.
If day is 7, print “Sunday”
DEFINITION
• An enum is a value type data type used to declare a list of named integer
constants.
• It can be defined using the enum keyword directly inside a namespace, class, or
structure.
• The enum is used to give a name to each constant so that the constant integer
can be read using its name.
• The enum can include named constants of numeric data type e.g. Byte, sbyte,
short, ushort, int, uint, long, or ulong.
EXAMPLE
enum WeekDays
{
Monday = 0,
Tuesday =1,
Wednesday = 2,
Thursday = 3,
Friday = 4,
Saturday =5,
Sunday = 6
}
Console.WriteLine(WeekDays.Fri
day);
Console.WriteLine((int)
WeekDays.Friday);
• type the code fragment within
your Main method
• What do you have on your
console window?
ENUMERATIONS…
Note this:
• By default, the first member of an enum
has the value 0 and the value of each
successive enum member is increased by
1.
• For example, in the following
enumeration, Monday is 0, Tuesday is 1,
Wednesday is 2.
• Edit the code from the previous slide to
what we have here
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
// put this within you Main Method
Console.WriteLine(WeekDays.Friday);
Console.WriteLine((int) WeekDays.Friday);
ENUMERATIONS…
• Try that and see what
happens!!!
enum WeekDays
{
Monday=14,
Tuesday,
Wednesday,
Thursday=16,
Friday,
Saturday,
Sunday,
}
// put this within you Main Method
Console.WriteLine(WeekDays.Friday);
Console.WriteLine((int) WeekDays.Friday);
ENUMERATIONS…
Note this:
• A change in the value of the first enum
member will automatically assign
incremental values to the other members
sequentially.
• For example, changing the value of
Monday to 14, will assign 15 to Tuesday, 16
to Wednesday, and so on:
• Edit the code from the previous slide to
what we have here.
enum WeekDays
{
Monday=14,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
Console.WriteLine(WeekDays.Friday);
Console.WriteLine((int) WeekDays.Friday);
Why DO YOU REALLY NEED ENUMS?
• Enum is mainly used to make code more readable by giving related
constants a meaningful name.
• It also improves maintainability.
ENUM METHODS
• Enum is an abstract class that includes static helper methods to work with enums.
Enum method Description
Format Converts the specified value of enum type to the
specified string format.
GetName Returns the name of the constant of the specified
value of specified enum.
GetNames Returns an array of string name of all the constant
of specified enum.
GetValues Returns an array of the values of all the constants of
specified enum.
object Parse(type, string) Converts the string representation of the name or
numeric value of one or more enumerated
constants to an equivalent enumerated object.
NOW TRY THEM OUT!
public static void Main(string[] args)
{
Console.WriteLine(Enum.GetName(typeof(WeekDays), 4));
Console.WriteLine("weekdays constant names:");
foreach (string str in Enum.GetNames(typeof(WeekDays)))
Console.WriteLine(str);
}
enum WeekDays
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
Sunday
}
// put this within you Main Method
Console.WriteLine(WeekDays.Friday);
Console.WriteLine((int) WeekDays.Friday);

More Related Content

PPTX
8enum in c#
PPTX
Lecture 6 Enumeration in java ADVANCE.pptx
PPTX
Enum
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
PPTX
Enumeration in c#
PDF
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
PPT
05a-enum.ppt
PPTX
Java enum
8enum in c#
Lecture 6 Enumeration in java ADVANCE.pptx
Enum
Enumeration in Java Explained | Java Tutorial | Edureka
Enumeration in c#
Diving in OOP (Day 6): Understanding Enums in C# (A Practical Approach)
05a-enum.ppt
Java enum

Similar to c# Enumerations (20)

PPTX
Enumerated data types
PPTX
Enumration datatype
PPT
User defined data type
PPT
enums
PPTX
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
PPT
Structure and Enum in c#
PPT
Effective Java - Enum and Annotations
PPTX
PDF
ENUM - make u r names as data types
PDF
Enum map
PPTX
C programming enumeration
PPTX
Enumeration type
PPTX
Powershell enum
ODP
Enum - Coding Guidelines
PDF
Data Types in C# OnlineTraining in Nareshit
PPTX
Enumeration In JAVA
PDF
Enum is your new best friend
PDF
15 1. enumeration
PPTX
PPTX
Enums_in_TypeScript_one_of_the_best_ppt.pptx
Enumerated data types
Enumration datatype
User defined data type
enums
21CS642 Module 1 Enumerations PPT.pptx VI SEM CSE 2021 Batch Students
Structure and Enum in c#
Effective Java - Enum and Annotations
ENUM - make u r names as data types
Enum map
C programming enumeration
Enumeration type
Powershell enum
Enum - Coding Guidelines
Data Types in C# OnlineTraining in Nareshit
Enumeration In JAVA
Enum is your new best friend
15 1. enumeration
Enums_in_TypeScript_one_of_the_best_ppt.pptx
Ad

More from Micheal Ogundero (15)

PDF
static methods
PDF
csharp repitition structures
PDF
selection structures
PDF
escape sequences and substitution markers
PDF
A simple program C# program
PDF
c# operators
PPTX
datatypes_variables_constants
PPTX
2 robot types_classifications
PDF
History of robots
PPTX
c# keywords, identifiers and Naming Conventions
PDF
Dictionary and sets-converted
PPTX
Basic Sorting algorithms csharp
PDF
Csharp_List
PDF
csharp_Passing_parameters_by_value_and_reference
PPTX
C# Value Data Types and Reference Data Types
static methods
csharp repitition structures
selection structures
escape sequences and substitution markers
A simple program C# program
c# operators
datatypes_variables_constants
2 robot types_classifications
History of robots
c# keywords, identifiers and Naming Conventions
Dictionary and sets-converted
Basic Sorting algorithms csharp
Csharp_List
csharp_Passing_parameters_by_value_and_reference
C# Value Data Types and Reference Data Types
Ad

Recently uploaded (20)

PDF
Getting Started with Data Integration: FME Form 101
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
A Presentation on Artificial Intelligence
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Spectroscopy.pptx food analysis technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
Getting Started with Data Integration: FME Form 101
The Rise and Fall of 3GPP – Time for a Sabbatical?
gpt5_lecture_notes_comprehensive_20250812015547.pdf
A Presentation on Artificial Intelligence
Dropbox Q2 2025 Financial Results & Investor Presentation
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
20250228 LYD VKU AI Blended-Learning.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Group 1 Presentation -Planning and Decision Making .pptx
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf

c# Enumerations

  • 1. C# ENUMERATIONS ENGR. MICHEAL OGUNDERO You can contact me via PHONE - +2348087365099 SKYPE – OGUNDERO MICHEAL Email – ogunderoayodeji@gmail.com
  • 2. A QUICK ONE! • Write a program that reads an input (integer), day from a user and prints to the console based on the if conditional statements below: If day is 1, print “Monday” If day is 2, print “Tuesday” . . . If day is 7, print “Sunday”
  • 3. DEFINITION • An enum is a value type data type used to declare a list of named integer constants. • It can be defined using the enum keyword directly inside a namespace, class, or structure. • The enum is used to give a name to each constant so that the constant integer can be read using its name. • The enum can include named constants of numeric data type e.g. Byte, sbyte, short, ushort, int, uint, long, or ulong.
  • 4. EXAMPLE enum WeekDays { Monday = 0, Tuesday =1, Wednesday = 2, Thursday = 3, Friday = 4, Saturday =5, Sunday = 6 } Console.WriteLine(WeekDays.Fri day); Console.WriteLine((int) WeekDays.Friday); • type the code fragment within your Main method • What do you have on your console window?
  • 5. ENUMERATIONS… Note this: • By default, the first member of an enum has the value 0 and the value of each successive enum member is increased by 1. • For example, in the following enumeration, Monday is 0, Tuesday is 1, Wednesday is 2. • Edit the code from the previous slide to what we have here enum WeekDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } // put this within you Main Method Console.WriteLine(WeekDays.Friday); Console.WriteLine((int) WeekDays.Friday);
  • 6. ENUMERATIONS… • Try that and see what happens!!! enum WeekDays { Monday=14, Tuesday, Wednesday, Thursday=16, Friday, Saturday, Sunday, } // put this within you Main Method Console.WriteLine(WeekDays.Friday); Console.WriteLine((int) WeekDays.Friday);
  • 7. ENUMERATIONS… Note this: • A change in the value of the first enum member will automatically assign incremental values to the other members sequentially. • For example, changing the value of Monday to 14, will assign 15 to Tuesday, 16 to Wednesday, and so on: • Edit the code from the previous slide to what we have here. enum WeekDays { Monday=14, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Console.WriteLine(WeekDays.Friday); Console.WriteLine((int) WeekDays.Friday);
  • 8. Why DO YOU REALLY NEED ENUMS? • Enum is mainly used to make code more readable by giving related constants a meaningful name. • It also improves maintainability.
  • 9. ENUM METHODS • Enum is an abstract class that includes static helper methods to work with enums. Enum method Description Format Converts the specified value of enum type to the specified string format. GetName Returns the name of the constant of the specified value of specified enum. GetNames Returns an array of string name of all the constant of specified enum. GetValues Returns an array of the values of all the constants of specified enum. object Parse(type, string) Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
  • 10. NOW TRY THEM OUT! public static void Main(string[] args) { Console.WriteLine(Enum.GetName(typeof(WeekDays), 4)); Console.WriteLine("weekdays constant names:"); foreach (string str in Enum.GetNames(typeof(WeekDays))) Console.WriteLine(str); } enum WeekDays { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } // put this within you Main Method Console.WriteLine(WeekDays.Friday); Console.WriteLine((int) WeekDays.Friday);