SlideShare a Scribd company logo
1 of 7
Enumeration in C#
Dr. Neeraj Kumar Pandey
Enumeration
 An enumeration is a user defined integer type which provides a way for
attaching names to numbers.
 The enum keyword automatically enumerates a list of words by assigning
them values 0,1,2….
enum shape
{
Circle,
Square,
Triangle
}
 This can be written as:- enum shape{ Circle,Square,Triangle}
 Here ,Circle has the value 0,Square has the value 1 and Triangle has the
value 2.
Enumeration Program
class EnumTest
{
public static void Main()
{
Area area= new Area();
area.AreaShape(15,Area.Shape.Circle);
area.AreaShape(15,Area.Shape.Square);
area.AreaShape(15,(Area.Shape)1);
area.AreaShape(15,(Area.Shape) 10);
}
}
using System;
class Area
{
public enum Shape
{
Circle,
Square
}
public void AreaShape(int x, Shape shape)
{
double area;
switch(shape)
{
case Shape.Circle:
area= Math.PI*x*x;
Console.WriteLine("Circle Area="+area);
break;
case Shape.Square:
area=x*x;
Console.WriteLine("Square Area="+area);
break;
default:
Console.WriteLine("Invalid Input");
break;
}
}
}
Enumeration Initialization
 Default value of the first enum member is 0 and that of each subsequent
is incremented by one.
 We can assign specific values for different members.
enum color
{
Red=4,
Green=Red+3,
Black=Red+Green+2,
Yellow=5
}
enum color
{
Red=4,
Green=9,
Black=3,
Yellow=5
}
Enumeration Initialization
 If the declaration of an enum member has no initializer, then its value is set implicitly as
follows:-
• If it is the first member, its value is zero.
• Otherwise, its value is obtained by adding one to the value of the previous member.
Consider the following enum declaration:-
enum Alphabet
{
A,
B=5,
C,
D=20,
E
}
Following enum declaration is not
valid because the declarations are
circular
enum ABC
{
A,
B=C,
C
}
Enumerator Base Type
 By default, the enum type is int. However, we can declare explicitly a base
type for each enum.
 The valid base type for enum are:- byte,sbyte,short,ushort,int,uint,long
and ulong.
enum Position : byte
{
Off,
On
}
enum shape: long
{
Circle,
Square=100,
Rectangle
}
Enumerator Type Conversion
 Enum types can be converted to their base type and back again with an
explicit conversion using a cast.
Enum values
{
value0,
value1,
value2,
value3
}
values u1=(values) 1;
int a= (int)u1;

More Related Content

What's hot (20)

CSharp Presentation
CSharp PresentationCSharp Presentation
CSharp Presentation
 
04. Console Input Output
04. Console Input Output 04. Console Input Output
04. Console Input Output
 
Effective Java - Enum and Annotations
Effective Java - Enum and AnnotationsEffective Java - Enum and Annotations
Effective Java - Enum and Annotations
 
C#.NET
C#.NETC#.NET
C#.NET
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Graphics programming in Java
Graphics programming in JavaGraphics programming in Java
Graphics programming in Java
 
Java swing
Java swingJava swing
Java swing
 
Generics
GenericsGenerics
Generics
 
Inheritance C#
Inheritance C#Inheritance C#
Inheritance C#
 
20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles20. Object-Oriented Programming Fundamental Principles
20. Object-Oriented Programming Fundamental Principles
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Object-Oriented Programming with C#
Object-Oriented Programming with C#Object-Oriented Programming with C#
Object-Oriented Programming with C#
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Oops concept on c#
Oops concept on c#Oops concept on c#
Oops concept on c#
 
C# String
C# StringC# String
C# String
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
 

Similar to Enumeration in c#

Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++Osama Al-Mohaia
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfarchgeetsenterprises
 
C# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdfC# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdfeyezoneamritsar
 
Trigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionTrigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionDlearAhmad
 
Modify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdfModify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdfhullibergerr25980
 
The purpose of this assignment is to have you write a Java program us.docx
 The purpose of this assignment is to have you write a Java program us.docx The purpose of this assignment is to have you write a Java program us.docx
The purpose of this assignment is to have you write a Java program us.docxKomlin1
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Palak Sanghani
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Palak Sanghani
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanationHarish Gyanani
 
Adding Uncertainty and Units to Quantity Types in Software Models
Adding Uncertainty and Units to Quantity Types in Software ModelsAdding Uncertainty and Units to Quantity Types in Software Models
Adding Uncertainty and Units to Quantity Types in Software ModelsTanja Mayerhofer
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdfRihabBENLAMINE
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEDrkhanchanaR
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfANJALIENTERPRISES1
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordLakshmi Sarvani Videla
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.pptMahyuddin8
 

Similar to Enumeration in c# (20)

Class program and uml in c++
Class program and uml in c++Class program and uml in c++
Class program and uml in c++
 
Need to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdfNeed to revise working code below,A good design means the applicat.pdf
Need to revise working code below,A good design means the applicat.pdf
 
C# example (Polymorphesim)
C# example (Polymorphesim)C# example (Polymorphesim)
C# example (Polymorphesim)
 
C# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdfC# I need assitance on my code.. Im getting an error message Us.pdf
C# I need assitance on my code.. Im getting an error message Us.pdf
 
Trigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum functionTrigonometric, hyperbolic functions with invers and sum function
Trigonometric, hyperbolic functions with invers and sum function
 
Modify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdfModify your solution for PLP04 to allow the user to choose the shape.pdf
Modify your solution for PLP04 to allow the user to choose the shape.pdf
 
The purpose of this assignment is to have you write a Java program us.docx
 The purpose of this assignment is to have you write a Java program us.docx The purpose of this assignment is to have you write a Java program us.docx
The purpose of this assignment is to have you write a Java program us.docx
 
Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]Lec 9 05_sept [compatibility mode]
Lec 9 05_sept [compatibility mode]
 
Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]Lec 8 03_sept [compatibility mode]
Lec 8 03_sept [compatibility mode]
 
40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation40+ examples of user defined methods in java with explanation
40+ examples of user defined methods in java with explanation
 
Ch3
Ch3Ch3
Ch3
 
Enums in c
Enums in cEnums in c
Enums in c
 
Adding Uncertainty and Units to Quantity Types in Software Models
Adding Uncertainty and Units to Quantity Types in Software ModelsAdding Uncertainty and Units to Quantity Types in Software Models
Adding Uncertainty and Units to Quantity Types in Software Models
 
Geometric model & curve
Geometric model & curveGeometric model & curve
Geometric model & curve
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
 
Oracle sql ppt2
Oracle sql ppt2Oracle sql ppt2
Oracle sql ppt2
 
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLEUnit 3 - Function & Grouping,Joins and Set Operations in ORACLE
Unit 3 - Function & Grouping,Joins and Set Operations in ORACLE
 
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdfCircle.javaimport java.text.DecimalFormat;public class Circle {.pdf
Circle.javaimport java.text.DecimalFormat;public class Circle {.pdf
 
Introduction to Data Science With R Lab Record
Introduction to Data Science With R Lab RecordIntroduction to Data Science With R Lab Record
Introduction to Data Science With R Lab Record
 
ch03-parameters-objects.ppt
ch03-parameters-objects.pptch03-parameters-objects.ppt
ch03-parameters-objects.ppt
 

More from Dr.Neeraj Kumar Pandey (18)

Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Structure in c#
Structure in c#Structure in c#
Structure in c#
 
Strings in c#
Strings in c#Strings in c#
Strings in c#
 
Operators and expression in c#
Operators and expression in c#Operators and expression in c#
Operators and expression in c#
 
Method parameters in c#
Method parameters in c#Method parameters in c#
Method parameters in c#
 
Dot net assembly
Dot net assemblyDot net assembly
Dot net assembly
 
Cloud introduction
Cloud introductionCloud introduction
Cloud introduction
 
Role of cloud computing in scm
Role of cloud computing in scmRole of cloud computing in scm
Role of cloud computing in scm
 
Public cloud
Public cloudPublic cloud
Public cloud
 
cloud computing Multi cloud
cloud computing Multi cloudcloud computing Multi cloud
cloud computing Multi cloud
 
Ibm bluemix case study
Ibm bluemix case studyIbm bluemix case study
Ibm bluemix case study
 
Business cases for the need of cloud computing
Business cases for the need of cloud computingBusiness cases for the need of cloud computing
Business cases for the need of cloud computing
 
cloud computing:Types of virtualization
cloud computing:Types of virtualizationcloud computing:Types of virtualization
cloud computing:Types of virtualization
 
cloud computing: Vm migration
cloud computing: Vm migrationcloud computing: Vm migration
cloud computing: Vm migration
 
Cloud Computing: Virtualization
Cloud Computing: VirtualizationCloud Computing: Virtualization
Cloud Computing: Virtualization
 
Dot net introduction
Dot net introductionDot net introduction
Dot net introduction
 
C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#C# lecture 2: Literals , Variables and Data Types in C#
C# lecture 2: Literals , Variables and Data Types in C#
 
C# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net FrameworkC# lecture 1: Introduction to Dot Net Framework
C# lecture 1: Introduction to Dot Net Framework
 

Recently uploaded

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 

Recently uploaded (20)

Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 

Enumeration in c#

  • 1. Enumeration in C# Dr. Neeraj Kumar Pandey
  • 2. Enumeration  An enumeration is a user defined integer type which provides a way for attaching names to numbers.  The enum keyword automatically enumerates a list of words by assigning them values 0,1,2…. enum shape { Circle, Square, Triangle }  This can be written as:- enum shape{ Circle,Square,Triangle}  Here ,Circle has the value 0,Square has the value 1 and Triangle has the value 2.
  • 3. Enumeration Program class EnumTest { public static void Main() { Area area= new Area(); area.AreaShape(15,Area.Shape.Circle); area.AreaShape(15,Area.Shape.Square); area.AreaShape(15,(Area.Shape)1); area.AreaShape(15,(Area.Shape) 10); } } using System; class Area { public enum Shape { Circle, Square } public void AreaShape(int x, Shape shape) { double area; switch(shape) { case Shape.Circle: area= Math.PI*x*x; Console.WriteLine("Circle Area="+area); break; case Shape.Square: area=x*x; Console.WriteLine("Square Area="+area); break; default: Console.WriteLine("Invalid Input"); break; } } }
  • 4. Enumeration Initialization  Default value of the first enum member is 0 and that of each subsequent is incremented by one.  We can assign specific values for different members. enum color { Red=4, Green=Red+3, Black=Red+Green+2, Yellow=5 } enum color { Red=4, Green=9, Black=3, Yellow=5 }
  • 5. Enumeration Initialization  If the declaration of an enum member has no initializer, then its value is set implicitly as follows:- • If it is the first member, its value is zero. • Otherwise, its value is obtained by adding one to the value of the previous member. Consider the following enum declaration:- enum Alphabet { A, B=5, C, D=20, E } Following enum declaration is not valid because the declarations are circular enum ABC { A, B=C, C }
  • 6. Enumerator Base Type  By default, the enum type is int. However, we can declare explicitly a base type for each enum.  The valid base type for enum are:- byte,sbyte,short,ushort,int,uint,long and ulong. enum Position : byte { Off, On } enum shape: long { Circle, Square=100, Rectangle }
  • 7. Enumerator Type Conversion  Enum types can be converted to their base type and back again with an explicit conversion using a cast. Enum values { value0, value1, value2, value3 } values u1=(values) 1; int a= (int)u1;