diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs
index 675e83fe050909..d6212cd0bcf78b 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs
@@ -7,6 +7,7 @@ namespace System.Diagnostics
// hardware supports it. Otherwise, the class will fall back to DateTime
// and uses ticks as a measurement.
+ [DebuggerDisplay("{DebuggerDisplay,nq}")]
public partial class Stopwatch
{
private const long TicksPerMillisecond = 10000;
@@ -150,5 +151,7 @@ private long GetElapsedDateTimeTicks()
// convert high resolution perf counter to DateTime ticks
return unchecked((long)(GetRawElapsedTicks() * s_tickFrequency));
}
+
+ private string DebuggerDisplay => $"{Elapsed} (IsRunning = {_isRunning})";
}
}
diff --git a/src/libraries/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj b/src/libraries/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj
index 1e78f1533949d6..3e3e293778105c 100644
--- a/src/libraries/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj
+++ b/src/libraries/System.Runtime.Extensions/tests/System.Runtime.Extensions.Tests.csproj
@@ -82,6 +82,7 @@
+
diff --git a/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs b/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs
index 02226d4abe9ef2..273358d1a9f1dc 100644
--- a/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs
+++ b/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
+using System.Reflection;
using System.Threading;
using Xunit;
@@ -95,6 +96,27 @@ public static void StartNewAndRestart()
}
}
+ [Fact]
+ public static void DebuggerAttributesValid()
+ {
+ DebuggerAttributes.ValidateDebuggerDisplayReferences(new Stopwatch());
+
+ Stopwatch watch = new Stopwatch();
+ Assert.Equal("00:00:00 (IsRunning = False)", GetDebuggerDisplayProperty(watch));
+ watch.Start();
+ Thread.Sleep(10);
+ Assert.Contains("(IsRunning = True)", GetDebuggerDisplayProperty(watch));
+ Assert.DoesNotContain("00:00:00 ", GetDebuggerDisplayProperty(watch));
+ watch.Stop();
+ Assert.Contains("(IsRunning = False)", GetDebuggerDisplayProperty(watch));
+ Assert.DoesNotContain("00:00:00 ", GetDebuggerDisplayProperty(watch));
+
+ static string GetDebuggerDisplayProperty(Stopwatch value)
+ {
+ return (string)typeof(Stopwatch).GetProperty("DebuggerDisplay", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(value);
+ }
+ }
+
[OuterLoop("Sleeps for relatively long periods of time")]
[Fact]
public static void ElapsedMilliseconds_WithinExpectedWindow()