Enum and Nullable Types Ashima Wadhwa Enumerations Enumerations
Enum and Nullable Types Ashima Wadhwa
Enumerations, • Enumerations, or enums, are used to group named constants similar to how they are used in C and C++. • In C#, enums are value types, and enum constants must be integral numeric values. The To. String method can be used to print out string representations of the named constants.
The general syntax for declaring an enumeration is: • • enum <enum_name> { enumeration list };
Example enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat };
using System; namespace Enum. Application { class Enum. Program { enum Days { Sun, Mon, tue, Wed, thu, Fri, Sat }; static void Main(string[] args) { int Weekday. Start = (int)Days. Mon; int Weekday. End = (int)Days. Fri; Console. Write. Line("Monday: {0}", Weekday. Start); Console. Write. Line("Friday: {0}", Weekday. End); Console. Read. Key(); } }
Output Monday: 1 Friday: 5
For Each Loop • foreach loop is a different kind of looping constructs in C# programming that doesn’t includes initialization, termination and increment/decrement characteristics. It uses collection to take value one by one and then processes them.
syntax: foreach (string name in arr) { } Where, name is a string variable that takes value from collection as arr and then processes them in the body area.
using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace foreach_loop { class Program { static void Main(string[] args) { string[] arr = new string[5]; // declaring array
//Storing value in array element arr[0] = "Steven"; arr[1] = "Clark"; arr[2] = "Mark"; arr[3] = "Thompson"; arr[4] = "John"; } } //retrieving value using foreach loop foreach (string name in arr) { Console. Write. Line("Hello " + name); } Console. Read. Line();
Output Hello Steven Hello Clark Hello Mark Hello Thompson Hello John
The following example defines a simple Color enumeration in C#. public enum Color { Green, //defaults to 0 Orange, //defaults to 1 Red, //defaults to 2 Blue //defaults to 3 }
using System; public enum Color { Green, //defaults to 0 Orange, //defaults to 1 Red, //defaults to 2 Blue //defaults to 3 } class Test. Enums { static void Main() { System. Console. Write. Line("Possible color choices: "); //Enum. Get. Names returns a string array of named constants for the enum. foreach(string s in System. Enum. Get. Names(typeof(Color))) { System. Console. Write. Line(s); } Color favorite = Color. Blue; System. Console. Write. Line("Favorite Color is {0}", favorite); System. Console. Write. Line("Favorite Color value is {0}", (int) favorite); } }
Nullable Types • C# provides a special data types, the nullable types, to which you can assign normal range of values as well as null values. • For example, you can store any value from 2, 147, 483, 648 to 2, 147, 483, 647 or null in a Nullable<Int 32> variable. • Similarly, you can assign true, false, or null in a Nullable<bool> variable. Syntax for declaring a nullable type is as follows:
using System; namespace Calculator. Application { class Nullables. At. Show { static void Main(string[] args) { int? num 1 = null; int? num 2 = 45; double? num 3 = new double? (); double? num 4 = 3. 14157; bool? boolval = new bool? (); // display the values Console. Write. Line("Nullables at Show: {0}, {1}, {2}, {3}", num 1, num 2, num 3, num 4); Console. Write. Line("A Nullable boolean value: {0}", boolval); Console. Read. Line(); } } }
When the above code is compiled and executed, it produces the following result: Nullables at Show: , 45, , 3. 14157 A Nullable boolean value:
Queries? ?
- Slides: 18