- What is Enumeration in C#?
- How to convert Enum to String in C#?
- How to convert Enum to List in C#?
- How can you loop through Enum values in C#?
- How can an int value cast to enum in C#?
- How to get int value from Enum in C#?
- How to convert String to Enum in C#?
- How to use Enum in Switch case in C#?
1. What is Enumeration in C#?
- An enum is a distinct value typewith a set of named constants
- It is declared by using keyword Enum
- Every Enum type has an underlying type which can be any integral type except char. The default underlying type is int.
- The first enumerator has the value 0 by default and the value of each successive enumerator is increased by 1
- Enumeration bridge the gap between numbers and objects
Example
|
enum colors
{
Red = 1,
Blue = 2,
Yellow = 3,
Green = 4
};
|
This code declares a colors enumeration containing four values.
2. How to convert Enum to String in C#?
You can convert Enum to String using ToString() method.
|
class Program
{
enum Colors { Red = 1, Pink = 2 };
static void Main(string[] args)
{
Enum myColors = Colors.Pink;
Console.WriteLine("The value of this instance is '{0}'",
myColors.ToString());
Console.ReadLine();
}
}
|
3. How to convert Enum to List in C#?
There are 2 ways of converting an Enum to a List in C#.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Program
{
enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };
static void Main(string[] args)
{
Array arr = Enum.GetValues(typeof(Days));
List<string> lstDays = new List<string>(arr.Length);
for (int i = 0; i < arr.Length; i++)
{
lstDays.Add(arr.GetValue(i).ToString());
}
}
}
|
Or
|
class Program
{
enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };
static void Main(string[] args)
{
var lstDays = Enum.GetNames(typeof(Days));
}
}
|
4. How can you iterate through Enum values in C#?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
class Program
{
public enum Colors
{
red,
blue,
green,
yellow
}
static void Main(string[] args)
{
foreach (Colors iColor in Enum.GetValues(typeof(Colors)))
{
Console.WriteLine(iColor.ToString());
}
}
}
|
5. How can an int value cast to Enum ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Program
{
enum Days
{
Sunday = 1,
TuesDay = 2,
wednesday = 3
}
static void Main(string[] args)
{
Days day = (Days)3;
Console.WriteLine(day);
}
}
|
or
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class Program
{
enum Days
{
Sunday = 1,
TuesDay = 2,
wednesday = 3
}
static void Main(string[] args)
{
Days day = (Days)Enum.ToObject(typeof(Days), 3);
Console.WriteLine(day);
}
}
//Output: wednesday
|
6. How to get int value from Enum in C#?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Program
{
enum Colors
{
Blue = 1,
Red = 2,
Yellow = 3,
Pink = 4
}
static void Main(string[] args)
{
int color = (int)Colors.Red;
Console.WriteLine(color);
}
}
//Output : 2
|
7.How to convert String to Enum in C#?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
class Program
{
enum Colors
{
Blue,
Red,
Yellow,
Pink
}
static void Main(string[] args)
{
string inVal = "Red";
Colors newColor = (Colors)Enum.Parse(typeof(Colors), inVal);
//Check the Enum type
if (newColor == Colors.Red)
{
Console.WriteLine(newColor.ToString());
}
}
}
|
8. How to use Enum in Switch case in C# ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
class Program
{
enum Colors
{
Blue,
Red,
Yellow,
Pink
}
static void Main(string[] args)
{
Colors value = Colors.Yellow;
switch (value)
{
case Colors.Blue:
Console.WriteLine("Color is Blue");
break;
case Colors.Red:
Console.WriteLine("Color is Red");
break;
case Colors.Yellow:
Console.WriteLine("Color is Yellow");
break;
}
}
}
|
Thanks for visiting !!
No comments:
Post a Comment