Tuesday 10 October 2017

C# Interview Questions on Enum

  1. What is Enumeration in C#?
  2. How to convert Enum to String in C#?
  3. How to convert Enum to List in C#?
  4. How can you loop through Enum values in C#?
  5. How can an int value cast to enum in C#?
  6. How to get int value from Enum in C#?
  7. How to convert String to Enum in C#?
  8. 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

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.

3. How to convert Enum to List in C#?

There are 2 ways of converting an Enum to a List in C#.

Or

4. How can you iterate through Enum values in C#?


5. How can an int value cast to Enum ?

or


6. How to get int value from Enum in C#?


7.How to convert String to Enum in C#?


8. How to use Enum in Switch case in C# ?

Thanks for visiting !!

No comments:

Post a Comment