TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Converting Enum to Different Format
Banketeshvar Narayan
Nov 19
2015
Code
809
0
2
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
//way 1
Enum MyNewEnum = (MyColors)Enum.Parse(
typeof
(MyColors),
"RED"
);
//Way 2
Enum MyNewEnum2 = (MyColors)Enum.Parse(
typeof
(MyColors),
"1"
);
//Way 3
Enum MyNewEnum3 = (MyColors)1;
//Way 4
Enum MyNewEnum4 = (MyColors)Enum.ToObject(
typeof
(MyColors), 1);
//Way 5
Enum MyNewEnum5 = (MyColors)3;
//Way 6
string
myEnumString = ((MyColors)1).ToString(
"F"
);
string
myEnumString2 = ((MyColors)1).ToString(
"F"
);
string
myEnumString3 = MyColors.BLUE.ToString(
"d"
);
string
myEnumString4 = MyColors.BLUE.ToString(
"D"
);
string
myEnumString5 = MyColors.BLUE.ToString(
"g"
);
string
myEnumString6 = MyColors.BLUE.ToString(
"G"
);
string
myEnumString7 = MyColors.BLUE.ToString(
"x"
);
string
myEnumString8 = MyColors.BLUE.ToString(
"X"
);
//Way 7
string
[] colorrList = Enum.GetNames(
typeof
(MyColors));
string
myEnumString9 = Enum.GetName(
typeof
(MyColors), 5);
//way 8
string
myFormatEnumString1 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"d"
);
string
myFormatEnumString2 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"D"
);
string
myFormatEnumString3 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"f"
);
string
myFormatEnumString4 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"F"
);
string
myFormatEnumString5 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"g"
);
string
myFormatEnumString6 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"G"
);
string
myFormatEnumString7 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"x"
);
string
myFormatEnumString8 = Enum.Format(
typeof
(MyColors), MyColors.ORANGE,
"X"
);
62
Converting Enum
Parsing Enum
enum to string