diff --git a/src/libraries/Common/src/System/Net/LazyAsyncResult.cs b/src/libraries/Common/src/System/Net/LazyAsyncResult.cs index 501edacb43051c..404effa3b10730 100644 --- a/src/libraries/Common/src/System/Net/LazyAsyncResult.cs +++ b/src/libraries/Common/src/System/Net/LazyAsyncResult.cs @@ -15,23 +15,8 @@ internal class LazyAsyncResult : IAsyncResult private const int ForceAsyncCount = 50; // This is to avoid user mistakes when they queue another async op from a callback the completes sync. - [ThreadStatic] - private static ThreadContext? t_threadContext; - - private static ThreadContext CurrentThreadContext - { - get - { - ThreadContext? threadContext = t_threadContext; - if (threadContext == null) - { - threadContext = new ThreadContext(); - t_threadContext = threadContext; - } - - return threadContext; - } - } + [field: ThreadStatic] + private static ThreadContext CurrentThreadContext => field ??= new(); private sealed class ThreadContext { diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ContractNameServices.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ContractNameServices.cs index d19b5da90618b3..df54ca7e9ec4a7 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ContractNameServices.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ContractNameServices.cs @@ -25,16 +25,8 @@ internal static class ContractNameServices private const char GenericFormatOpeningBracket = '{'; private const char GenericFormatClosingBracket = '}'; - [ThreadStatic] - private static Dictionary? typeIdentityCache; - - private static Dictionary TypeIdentityCache - { - get - { - return typeIdentityCache ??= new Dictionary(); - } - } + [field: ThreadStatic] + private static Dictionary TypeIdentityCache => field ??= new(); internal static string GetTypeIdentity(Type type) { diff --git a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs index 339aae535791db..26e2b2ffedc6df 100644 --- a/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs +++ b/src/libraries/System.ComponentModel.Composition/src/System/ComponentModel/Composition/ReflectionModel/ImportType.cs @@ -21,16 +21,8 @@ internal sealed class ImportType private Func? _castSingleValue; private readonly bool _isOpenGeneric; - [ThreadStatic] - internal static Dictionary?>? _castSingleValueCache; - - private static Dictionary?> CastSingleValueCache - { - get - { - return _castSingleValueCache ??= new Dictionary?>(); - } - } + [field: ThreadStatic] + private static Dictionary?> CastSingleValueCache => field ??= new(); public ImportType(Type type, ImportCardinality cardinality) { diff --git a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/RandomNumberGenerator.cs b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/RandomNumberGenerator.cs index 351e8ce6ad7e76..d212ecf507d54c 100644 --- a/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/RandomNumberGenerator.cs +++ b/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/RandomNumberGenerator.cs @@ -8,11 +8,10 @@ namespace System.Diagnostics /// internal sealed class RandomNumberGenerator { - [ThreadStatic] private static RandomNumberGenerator? t_random; - private ulong _s0, _s1, _s2, _s3; - public static RandomNumberGenerator Current => t_random ??= new RandomNumberGenerator(); + [field: ThreadStatic] + public static RandomNumberGenerator Current => field ??= new(); public unsafe RandomNumberGenerator() { diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs index 2ec074ebbf6a4a..1fddba5fab6569 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Debug.cs @@ -32,15 +32,14 @@ public static bool AutoFlush set { } } - [ThreadStatic] - private static int t_indentLevel; + [field: ThreadStatic] public static int IndentLevel { - get => t_indentLevel; + get => field; set { - t_indentLevel = value < 0 ? 0 : value; - s_provider.OnIndentLevelChanged(t_indentLevel); + field = value < 0 ? 0 : value; + s_provider.OnIndentLevelChanged(field); } } diff --git a/src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs b/src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs index 08eb9283ab12b3..3c4d115bb83e90 100644 --- a/src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs +++ b/src/libraries/System.Security.Cryptography.Cose/tests/CoseTestHelpers.cs @@ -464,9 +464,8 @@ private static void AssertHeaders(CborReader reader, List<(CoseHeaderLabel, Read } } - [ThreadStatic] - private static TestKeyRing? t_threadKeys; - private static TestKeyRing ThreadKeys => (t_threadKeys ??= new TestKeyRing()); + [field: ThreadStatic] + private static TestKeyRing ThreadKeys => field ??= new(); internal static ECDsa ES256 => ThreadKeys.ES256; internal static ECDsa ES384 => ThreadKeys.ES384; diff --git a/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs b/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs index af4014e97065ff..d2470b777c565f 100644 --- a/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs +++ b/src/libraries/System.Transactions.Local/src/System/Transactions/Transaction.cs @@ -1126,31 +1126,29 @@ internal sealed class ContextData internal bool _asyncFlow; - [ThreadStatic] - private static ContextData? t_staticData; - internal ContextData(bool asyncFlow) { _asyncFlow = asyncFlow; } [AllowNull] + [field: ThreadStatic] internal static ContextData TLSCurrentData { - get => t_staticData ??= new ContextData(false); + get => field ??= new(false); set { - if (value == null && t_staticData != null) + if (value == null && field != null) { // set each property to null to retain one TLS ContextData copy. - t_staticData.CurrentScope = null; - t_staticData.CurrentTransaction = null; - t_staticData.DefaultComContextState = DefaultComContextState.Unknown; - t_staticData.WeakDefaultComContext = null; + field.CurrentScope = null; + field.CurrentTransaction = null; + field.DefaultComContextState = DefaultComContextState.Unknown; + field.WeakDefaultComContext = null; } else { - t_staticData = value; + field = value; } } }