Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1017807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:45:37+00:00 2026-05-16T10:45:37+00:00

class Program { static void Main(string[] args) { string value = "12345"; Type enumType

  • 0
class Program
{
    static void Main(string[] args)
    {
        string value = "12345";
        Type enumType = typeof(Fruits);
        Fruits fruit = Fruits.Apple;
        try
        {
            fruit = (Fruits) Enum.Parse(enumType, value);
        }
        catch (ArgumentException)
        {
            Console.WriteLine(String.Format("{0} is no healthy food.", value));
        }
        Console.WriteLine(String.Format("You should eat at least one {0} per day.", fruit));
        Console.ReadKey();
    }

    public enum Fruits
    {
        Apple,
        Banana,
        Orange
    }
}

If you execute the code above the result shows:

You should eat at least one 12345 per day.

I really expected an ArgumentException to be thrown if a unknown name (string) is passed. Taking a close look at the Enum.Parse definition reveals:

Summary:
Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Exceptions:
ArgumentException: enumType is not an Enum. -or- value is either an empty string or only contains white space. -or- value is a name, but not one of the named constants defined for the enumeration.

I.e. if a string representation of an integer is passed, a new enum value is created and now exception is thrown by design. Does this make sense?

At least I now know to call Enum.IsDefined(enumType, value) prior to Enum.Parse()

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:45:37+00:00Added an answer on May 16, 2026 at 10:45 am

    The “named constant” is the textual representation of an Enum’s value, not the number that you’ve assigned to it.

    If you change:

    string value = "12345";
    

    To:

    string value = "Cake";
    

    You’ll see the error you’re expecting, because “value is a name, but not one of the named constants defined for the enumeration.”. In this instance the value you’re passing in is a name, “Cake”, but not one in the enumeration.

    Think of Enum.Parse(enumType, value); doing the following:

    1. If value is a null reference, throw an ArgumentNullException
    2. Is the value in value one of the named constants in the enumeration in enumType. If yes, return that value from the enumeration and stop.
    3. Is the value in value directly convertible to the underlying type (in this instance Int32), if yes, return that value and stop (even if there’s no named constant for that value).
    4. Is the value in value directly convertible to the underlying type, but outside of the range of the underlying type? e.g. the value is a string containing a number one greater than MAXINT. If yes, throw an OverflowException.
    5. Is the value not castable to the underlying type? If yes, throw an ArgumentException.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Program { static void Main(string[] args) { String value = Two; Type enumType
class Program { static void Main(string[] args) { List<Book> books = new List<Book> {
I have below code: class Program { static void Main(string[] args) { Task[] tasks
i have written the following code class Program { static void Main(string[] args) {
I have the following code: class Program { static void Main(string[] args) { new
I am trying to do the following: class Program { static void Main(string[] args)
First look at this code: class Program { static void Main(string[] args) { var
What is the workaround to this problem? class Program { static void Main(string[] args)
Suppose public class Program { public static void Main(string[] args) { Program p =
using System; namespace FirstApplication { class Program { static void Main(params string[] args) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.