Skip to content

Commit e372a3e

Browse files
committed
Minor change
1 parent b9d601c commit e372a3e

File tree

1 file changed

+31
-41
lines changed

1 file changed

+31
-41
lines changed

src/runtime/Types/EnumObject.cs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -99,49 +99,39 @@ private static bool TryCompare(Enum left, object right, out int result)
9999
result = int.MinValue;
100100
var conversionSuccessful = true;
101101
var leftType = left.GetType();
102-
var rightType = right.GetType();
103-
// Same enum comparison:
104-
if (leftType == rightType)
105-
{
106-
result = left.CompareTo(right);
107-
}
108-
// Comparison with other enum types
109-
else if (rightType.IsEnum)
110-
{
111-
var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64);
112-
result = Compare(left, right as Enum, leftIsUnsigned);
113-
}
114-
else if (right is string rightString)
115-
{
116-
result = left.ToString().CompareTo(rightString);
117-
}
118-
else
102+
var leftIsUnsigned = () => leftType.GetEnumUnderlyingType() == typeof(UInt64);
103+
104+
switch (right)
119105
{
120-
var leftIsUnsigned = leftType.GetEnumUnderlyingType() == typeof(UInt64);
121-
if (right is double rightDouble)
122-
{
123-
result = Compare(left, rightDouble, leftIsUnsigned);
124-
}
125-
else if (right is long rightLong)
126-
{
127-
result = Compare(left, rightLong, leftIsUnsigned);
128-
}
129-
else if (right is ulong rightULong)
130-
{
131-
result = Compare(left, rightULong, leftIsUnsigned);
132-
}
133-
else if (right is int rightInt)
134-
{
135-
result = Compare(left, rightInt, leftIsUnsigned);
136-
}
137-
else if (right is uint rightUInt)
138-
{
139-
result = Compare(left, rightUInt, leftIsUnsigned);
140-
}
141-
else
142-
{
106+
case Enum when leftType == right.GetType():
107+
// Same enum type
108+
result = left.CompareTo(right);
109+
break;
110+
case Enum rightEnum:
111+
// Different enum type
112+
result = Compare(left, rightEnum, leftIsUnsigned());
113+
break;
114+
case string rightString:
115+
result = left.ToString().CompareTo(rightString);
116+
break;
117+
case double rightDouble:
118+
result = Compare(left, rightDouble, leftIsUnsigned());
119+
break;
120+
case long rightLong:
121+
result = Compare(left, rightLong, leftIsUnsigned());
122+
break;
123+
case ulong rightULong:
124+
result = Compare(left, rightULong, leftIsUnsigned());
125+
break;
126+
case int rightInt:
127+
result = Compare(left, (long)rightInt, leftIsUnsigned());
128+
break;
129+
case uint rightUInt:
130+
result = Compare(left, (ulong)rightUInt, leftIsUnsigned());
131+
break;
132+
default:
143133
conversionSuccessful = false;
144-
}
134+
break;
145135
}
146136

147137
return conversionSuccessful;

0 commit comments

Comments
 (0)