-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
Background and motivation
ConsoleModifiers is a flags enum. Per our guidelines, they should have a value for 0, which is usually called None.
Since the value is missing, developers need to compare the enum to the literal 0. This doesn't require a cast because in C# the literal 0 can be implicitly converted into any enum value, but this isn't intuitive. It also means when inspecting the value in the debugger, one sees the value 0 instead of a more friendly text None.
And when authoring switch statements and letting the IDE generate cases, there is no explicit case label for the zero case. Combined with general code completion, it can confuse developers because it's not immediately clear how absent modifiers would be represented.
The same applies to ConsoleKey. While it's not a flags-based enum some characters don't have a key and will only come through in form of ConsoleKeyInfo.KeyChar, such as ;. In those cases, ConsoleKeyInfo.Key is 0.
API Proposal
namespace System;
[Flags]
public enum ConsoleModifiers
{
None = 0,
// Existing:
//
// Alt = 1,
// Shift = 2,
// Control = 4
}
public enum ConsoleKey
{
None = 0,
// Existing values omitted for brevity
}API Usage
private void HandleKey(ConsoleKeyInfo info)
{
switch (info.Modifiers, info.Key)
{
case (ConsoleModifiers.None, ConsoleKey.Q):
case (ConsoleModifiers.None, ConsoleKey.Escape):
Application.Quit();
break;
case (ConsoleModifiers.None, ConsoleKey.N):
GotoNextSearchHit();
break;
case (ConsoleModifiers.Shift, ConsoleKey.N):
GotoPreviousSearchHit();
break;
case (_, ConsoleKey.None):
HandleTyping(info.KeyChar);
break;
}
}Alternative Designs
No response
Risks
No response