Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Performance improvements for Enum.ToString() #3849

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/mscorlib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private static String InternalFormat(RuntimeType eT, Object value)
if (!eT.IsDefined(typeof(System.FlagsAttribute), false)) // Not marked with Flags attribute
{
// Try to see if its one of the enum values, then we return a String back else the value
String retval = GetName(eT, value);
String retval = eT.GetEnumName(value);
if (retval == null)
return value.ToString();
else
Expand Down
5 changes: 3 additions & 2 deletions src/mscorlib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,12 +1537,13 @@ public virtual string GetEnumName(object value)
if (!(valueType.IsEnum || Type.IsIntegerType(valueType)))
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");

Array values = GetEnumRawConstantValues();
string[] names;
Array values;
GetEnumData(out names, out values);
int index = BinarySearch(values, value);

if (index >= 0)
{
string[] names = GetEnumNames();
return names[index];
}

Expand Down