To compare strings with enums the right way, one should use enum.parse rather than comparing string literals. Following is an example where i am parsing a string type variable named messageId and creating an enum of type MessageType

MessageType messageType = (MessageType) System.Enum.Parse(typeof(MessageType),messageId,true);

Last argument in System.Enum.Parse specifies if the enumeration/comparison should be case sensitive or not.

2 Comments

  1. Reading your blog and I figured you’d be interested in advancing your life a bit, call us at 1-206-339-5106. No tests, books or exams, easiest way to get a Bachelors, Masters, MBA, Doctorate or Ph.D in almost any field.

    Totally confidential, open 24 hours a day.

    Hope to hear from you soon!

  2. It’s the most funniest way i had ever seen to make an ad for an university degree 😀

Leave a Reply

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