Cast int to enum in C#

In this post I want to present an easy way to cast an int to an enum in C#. It is very easy and straight forward:

From a string:

YourEnum foo = (YourEnum) Enum.Parse(typeof(YourEnum), yourString);

From an int:

YourEnum foo = (YourEnum)yourInt;

 

That’s all! Have a nice day!

Daniel Zuidinga

.NET-Developer, Dipl.-Ing., Borussia Dortmund Supporter

Leave a Reply

Your email address will not be published. Required fields are marked *