diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs index ad0a6847d9c94c..62742587b69959 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs @@ -879,6 +879,8 @@ private static bool TryParseRareEnum(RuntimeType enumType, ReadOnlySpan va private static bool TryParseByName(RuntimeType enumType, ReadOnlySpan value, bool ignoreCase, bool throwOnFailure, out ulong result) { + ReadOnlySpan originalValue = value; + // Find the field. Let's assume that these are always static classes because the class is an enum. EnumInfo enumInfo = GetEnumInfo(enumType); string[] enumNames = enumInfo.Names; @@ -952,7 +954,7 @@ private static bool TryParseByName(RuntimeType enumType, ReadOnlySpan valu if (throwOnFailure) { - throw new ArgumentException(SR.Format(SR.Arg_EnumValueNotFound, value.ToString())); + throw new ArgumentException(SR.Format(SR.Arg_EnumValueNotFound, originalValue.ToString())); } result = 0; diff --git a/src/libraries/System.Runtime/tests/System/EnumTests.cs b/src/libraries/System.Runtime/tests/System/EnumTests.cs index f718e6e4c24429..a764555bd93fd8 100644 --- a/src/libraries/System.Runtime/tests/System/EnumTests.cs +++ b/src/libraries/System.Runtime/tests/System/EnumTests.cs @@ -287,6 +287,15 @@ private static void Parse_Generic_Invalid(Type enumType, string value, bool i } } + [Theory] + [InlineData("Yellow")] + [InlineData("Yellow,Orange")] + public static void Parse_NonExistentValue_IncludedInErrorMessage(string value) + { + ArgumentException e = Assert.Throws(() => Enum.Parse(typeof(SimpleEnum), value)); + Assert.Contains(value, e.Message); + } + [Theory] [InlineData(SByteEnum.Min, "Min")] [InlineData(SByteEnum.One, "One")]