0% found this document useful (0 votes)
51 views8 pages

Enumerations: System System - Collections.Generic System - Text Consoleapplication15 (

Enumerations allow defining a custom data type that consists of a set of named constants. The enum keyword is used to define an enumeration. It assigns integer values starting from 0 to the enum members. The values can also be explicitly set. Enumerations provide a way to attach names to integer constants, making code more readable and reducing errors.

Uploaded by

Archu Deoli
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views8 pages

Enumerations: System System - Collections.Generic System - Text Consoleapplication15 (

Enumerations allow defining a custom data type that consists of a set of named constants. The enum keyword is used to define an enumeration. It assigns integer values starting from 0 to the enum members. The values can also be explicitly set. Enumerations provide a way to attach names to integer constants, making code more readable and reducing errors.

Uploaded by

Archu Deoli
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Enumerations

*) is a user defined integer type which provides way for attaching names to numbers there by increasing the flexibility of the code *) The enum keyword enumerates list of words by assigning them values 0,1,2,3 and so on *) it can also be said that a new data type defined will just be collection of constant variable names *) Syntax for enum is enum Shape { Circle, Square, Triangle } Or enum Shape{Circle,Square,Triangle} *) Here Circle has value 0,Square has value 1 and so on *) Variables are known as identifier and constant values attached with it is known as literals *) Both identifier and literal values can be accessed for any enum object in a program Example
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication15 { class Program { static void Main(string[] args) { X.Shape d = 0; Console.WriteLine((int)d); Console.WriteLine(d);

Console.ReadKey(); } } class X { public enum Shape { Circle, Square, Triangle } } } Output:

Passing enum to function


using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication15 { class Enumtest { static void Main(string[] args) { Area a = new Area(); a.AreaShape(5, Area.Shape.Triangle); Console.WriteLine(a.area); Area b = new Area(); b.AreaShape(5, Area.Shape.Triangle,5);

Console.WriteLine(b.area); Area c = new Area(); c.AreaShape(5, (Area.Shape)2); Console.WriteLine(c.area); Console.ReadKey(); } } class Area { public double area; public enum Shape { Circle, Square, Triangle } public void AreaShape(int t, Shape shape, int i = 15) { switch (shape) { case Shape.Circle: area = Math.PI * t * t; break; case Shape.Square: area = t * t; break; case Shape.Triangle: area = 0.5*t*i; break; default: Console.Write("Invalid input"); break;

} } } } Output:

Enumerator Initilaization
*) By default the value of first enum member is set to 0 and that of each subsequent member is incremented by 1 *) How ever one may specific value for different members for example: enum Shape { Circle=1, Square=3, Triangle=5 }
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication15 { class Program { static void Main(string[] args) { X.Shape d = X.Shape.Square; Console.WriteLine((int)d); Console.WriteLine(d); Console.WriteLine((X.Shape)5); Console.ReadKey(); }

} class X { public enum Shape { Circle=1, Square=3, Triangle=5 } } }

Output:

*) We can use expression also as long as they use the already defined enum Member enum Shape { Circle=1, Square=Circle+2, Triangle=Circle+4 } *) Declarartion of enum member must not be circular enum Shape { Circle=1,

Square=Triangle, Triangle } is not a valid declaration

Enumerator Base Type


*) By default enumerator base type is int *) However explicitly base type of enum can be changed *) Possible Base Types could be byte, sbyte, short, ushort, int, uint, long, ulong
using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication15 { class Program { static void Main(string[] args) { X.Shape d = X.Shape.Square; Console.WriteLine((int)d);

Console.ReadKey(); } } class X { public enum Shape:byte { Circle = 255, Square //will create compile time error

} } }

Practice Question: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication15 { class Program { static void Main(string[] args) { X.Direction d = 0; int i = (int)d; Console.WriteLine(d); Console.WriteLine(i); X.Direction e = X.Direction.South; i = (int)e; Console.WriteLine(e); Console.WriteLine(i); X.Direction f = (X.Direction)10; i = (int)f; Console.WriteLine(f); Console.WriteLine(i); Console.ReadKey(); } } class X { public enum Direction { North, East=10, West, South

} } } Output:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy