ENUMS IN C# We’ll explore the power of enum and will cover - TopicsExpress



          

ENUMS IN C# We’ll explore the power of enum and will cover almost every scenario in which we can use enum. We’ll follow a practical approach of learning to understand this concept. Let’s start with the definition taken from MSDN: “The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. Usually it is best to define an enum directly within a namespace so that all classes in the namespace can access it with equal convenience. However, an enum can also be nested within a class or struct. By default, the first enumerator has the value 0, and the value of each successive enumerator is increased by 1. For example, in the following enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.” Enum plays almost the same responsibility as the class does, i.e., creating a new data type, and it exists at the same level as class, interfaces or structs. Just open your Visual Studio and add a console application named Enums. You’ll get Program.cs class. Note: Each and every code snippet in this article is tried and tested. Declare an enum at the same level as of Program class, call it as Color. ------------------ Program.cs ------------------ namespace Enums { class Program { static void Main(string[] args) { } } enum Color { Yellow, Blue, Brown, Green } } In the above mentioned example, we created a new datatype with the help of enum. The datatype is Color having four distinct values, Yellow, Blue, Brown and Green. The text that we write inside the declared enum could be anything of your wish; it just provides a custom enumerated list to you. -------------------------------------------------------------- Modify your main program as shown below: -------------------------------------------------------------- using System; namespace Enums { class Program { static void Main(string[] args) { Console.WriteLine(Color.Yellow); Console.ReadLine(); } } enum Color { Yellow, Blue, Brown, Green } } Run the program. --------------------- Output: Yellow --------------------- ------------------------------------------------------------------------------ Now just typecast Color.Yellow to int, what do we get? ------------------------------------------------------------------------------ using System; namespace Enums { class Program { static void Main(string[] args) { Console.WriteLine((int)Color.Yellow); Console.ReadLine(); } } enum Color { Yellow, Blue, Brown, Green } } -------------- Output: 0 -------------- We see that enum is called as static variables, so an enum can be considered here as static objects. Therefore other enums in the above example can be declared in the same way as Yellow, like Blue can be declared as Color.Blue. The output in the above two examples we see is 0 when we typecast and Yellow without typecasting, hence we see here that its behaviour is very similar to an array where Yellow has a value 0, similarly Blue has a value 1, Brown: 2, Green: 3. Therefore, when we do Color.Yellow, it’s like displaying a number 0, so from this can we infer that an enum represents a constant number, therefore an enum type is a distinct type having named constants. Point to remember: An enum represents for a constant number, and an enum type is known as a distinct type having named constants. Hope you enjoy the little tutorial.... Cheers
Posted on: Sat, 30 Aug 2014 06:27:37 +0000

Trending Topics



-10152126043002407">I got married on Saturday, June 14th (flag day), 1986. We didnt

Recently Viewed Topics




© 2015