Reputation: 343
public enum TimeOfDay
{
Morning = 0,
Afternoon = 1,
Evening = 2
}
Question: When we want to obtain an enum value from its string, we can use:
TimeOfDay time = TimeOfDay.Afternoon;
Console.WriteLine((int)time);
But what does maen the code below (with the same answer)?
TimeOfDay time2 = (TimeOfDay) Enum.Parse(typeof(TimeOfDay), "afternoon", true);
Console.WriteLine((int)time2);
Thank you, Mohsen
Upvotes: 0
Views: 131
Reputation: 21757
MSDN defines the Enum.Parse method as
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
That is, the method takes either the integer or string representation and returns corresponding object from the associated Enum. In this case, user supplies the string parameter "afternoon", Enum type "TimeOfDay" and sets the case insensitive flag to true. The method then does case-insensitive match against the objects in the Enum and returns the object which has name matching the user's parameter.
Upvotes: 3