SlideShare a Scribd company logo
1 of 10
Download to read offline
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

More from Micheal Ogundero

csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structuresMicheal Ogundero
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markersMicheal Ogundero
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# programMicheal Ogundero
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constantsMicheal Ogundero
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classificationsMicheal Ogundero
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming ConventionsMicheal Ogundero
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-convertedMicheal Ogundero
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharpMicheal Ogundero
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referenceMicheal Ogundero
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesMicheal Ogundero
 

More from Micheal Ogundero (14)

csharp repitition structures
csharp repitition structurescsharp repitition structures
csharp repitition structures
 
selection structures
selection structuresselection structures
selection structures
 
escape sequences and substitution markers
escape sequences and substitution markersescape sequences and substitution markers
escape sequences and substitution markers
 
A simple program C# program
A simple program C# programA simple program C# program
A simple program C# program
 
c# operators
c# operatorsc# operators
c# operators
 
datatypes_variables_constants
datatypes_variables_constantsdatatypes_variables_constants
datatypes_variables_constants
 
2 robot types_classifications
2 robot types_classifications2 robot types_classifications
2 robot types_classifications
 
History of robots
History of robotsHistory of robots
History of robots
 
c# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventionsc# keywords, identifiers and Naming Conventions
c# keywords, identifiers and Naming Conventions
 
Dictionary and sets-converted
Dictionary and sets-convertedDictionary and sets-converted
Dictionary and sets-converted
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Csharp_List
Csharp_ListCsharp_List
Csharp_List
 
csharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_referencecsharp_Passing_parameters_by_value_and_reference
csharp_Passing_parameters_by_value_and_reference
 
C# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data TypesC# Value Data Types and Reference Data Types
C# Value Data Types and Reference Data Types
 

Recently uploaded

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

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);