From 72c3c66b1dee2515fefe9474bd2248acf8f3ffe5 Mon Sep 17 00:00:00 2001 From: Atif Aziz Date: Sun, 29 Dec 2024 00:10:14 +0100 Subject: [PATCH] Allow use of C# 13 --- Directory.Build.props | 2 +- MoreLinq/Experimental/Memoize.cs | 4 +++- MoreLinq/Lock.cs | 8 ++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 MoreLinq/Lock.cs diff --git a/Directory.Build.props b/Directory.Build.props index 222a1c0a6..cecc99270 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 12 + 13 enable true 8.0-all diff --git a/MoreLinq/Experimental/Memoize.cs b/MoreLinq/Experimental/Memoize.cs index 8e6a3c6b5..40acdd15d 100644 --- a/MoreLinq/Experimental/Memoize.cs +++ b/MoreLinq/Experimental/Memoize.cs @@ -65,7 +65,7 @@ public static IEnumerable Memoize(this IEnumerable source) => sealed class MemoizedEnumerable(IEnumerable sequence) : IEnumerable, IDisposable { List? cache; - readonly object locker = new(); + readonly Lock locker = new(); readonly IEnumerable source = sequence ?? throw new ArgumentNullException(nameof(sequence)); IEnumerator? sourceEnumerator; int? errorIndex; @@ -77,7 +77,9 @@ public IEnumerator GetEnumerator() { lock (this.locker) { +#pragma warning disable CA1508 // Avoid dead conditional code if (this.cache == null) +#pragma warning restore CA1508 // Avoid dead conditional code { this.error?.Throw(); diff --git a/MoreLinq/Lock.cs b/MoreLinq/Lock.cs new file mode 100644 index 000000000..ce65fc442 --- /dev/null +++ b/MoreLinq/Lock.cs @@ -0,0 +1,8 @@ +#if NET9_0_OR_GREATER +// https://learn.microsoft.com/dotnet/csharp/language-reference/statements/lock#guidelines +global using Lock = System.Threading.Lock; +#else +// For why "System.Object" instead of "object", see: +// https://github.com/dotnet/runtime/issues/110242 +global using Lock = System.Object; +#endif