Skip to content

Commit 3458292

Browse files
authored
Avoid unnecessary duplication of fields in NullableBooleanBoxes (#6529)
A boxed boolean doesn't know whether it came from a nullable bool or a bool, so there's no need to maintain separate True/FalseBox objects for bool? vs bool, nor do we need to store a NullBox... that field will just be null.
1 parent 654b98b commit 3458292

File tree

3 files changed

+13
-42
lines changed

3 files changed

+13
-42
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ToolTipService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public static void SetBetweenShowDelay(DependencyObject element, int value)
544544
DependencyProperty.RegisterAttached("ShowsToolTipOnKeyboardFocus", // Name
545545
typeof(bool?), // Type
546546
typeof(ToolTipService), // Owner
547-
new FrameworkPropertyMetadata(NullableBooleanBoxes.NullBox)); // Default Value
547+
new FrameworkPropertyMetadata(null)); // Default Value
548548

549549
/// <summary>
550550
/// Gets the value of the ShowsToolTipOnKeyboardFocus property.
@@ -573,7 +573,7 @@ public static void SetShowsToolTipOnKeyboardFocus(DependencyObject element, bool
573573
{
574574
throw new ArgumentNullException("element");
575575
}
576-
element.SetValue(ShowsToolTipOnKeyboardFocusProperty, NullableBooleanBoxes.Box(value));
576+
element.SetValue(ShowsToolTipOnKeyboardFocusProperty, BooleanBoxes.Box(value));
577577
}
578578

579579
#endregion

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/tooltip.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public bool StaysOpen
354354
public bool? ShowsToolTipOnKeyboardFocus
355355
{
356356
get { return (bool?)GetValue(ShowsToolTipOnKeyboardFocusProperty); }
357-
set { SetValue(ShowsToolTipOnKeyboardFocusProperty, NullableBooleanBoxes.Box(value)); }
357+
set { SetValue(ShowsToolTipOnKeyboardFocusProperty, BooleanBoxes.Box(value)); }
358358
}
359359

360360
#endregion

src/Microsoft.DotNet.Wpf/src/WindowsBase/MS/Internal/KnownBoxes.cs

Lines changed: 10 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,25 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System;
65
using MS.Internal.WindowsBase;
76

87
namespace MS.Internal.KnownBoxes
98
{
109
[FriendAccessAllowed] // Built into Base, also used by Core and Framework.
1110
internal static class BooleanBoxes
1211
{
13-
internal static object TrueBox = true;
14-
internal static object FalseBox = false;
12+
internal static readonly object TrueBox = true;
13+
internal static readonly object FalseBox = false;
1514

16-
internal static object Box(bool value)
17-
{
18-
if (value)
19-
{
20-
return TrueBox;
21-
}
22-
else
23-
{
24-
return FalseBox;
25-
}
26-
}
27-
}
15+
internal static object Box(bool value) =>
16+
value ? TrueBox : FalseBox;
2817

29-
[FriendAccessAllowed] // Built into Base, also used by Core and Framework.
30-
internal static class NullableBooleanBoxes
31-
{
32-
internal static object TrueBox = (bool?)true;
33-
internal static object FalseBox = (bool?)false;
34-
internal static object NullBox = (bool?)null;
35-
36-
internal static object Box(bool? value)
37-
{
38-
if (value.HasValue)
39-
{
40-
if (value == true)
41-
{
42-
return TrueBox;
43-
}
44-
else
45-
{
46-
return FalseBox;
47-
}
48-
}
49-
else
18+
internal static object Box(bool? value) =>
19+
value switch
5020
{
51-
return NullBox;
52-
}
53-
}
21+
true => TrueBox,
22+
false => FalseBox,
23+
null => null,
24+
};
5425
}
5526
}

0 commit comments

Comments
 (0)