Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System.Threading
{
Expand Down Expand Up @@ -111,10 +112,18 @@ public static T EnsureInitialized<T>([NotNull] ref T? target, Func<T> valueFacto
/// <returns>The initialized variable</returns>
private static T EnsureInitializedCore<T>([NotNull] ref T? target, Func<T> valueFactory) where T : class
{
T value = valueFactory() ?? throw new InvalidOperationException(SR.Lazy_StaticInit_InvalidOperation);
T? value = valueFactory();
if (value is null)
{
Throw();
}

Comment on lines +115 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
T? value = valueFactory();
if (value is null)
{
Throw();
}
if (valueFactory() is not { } value)
{
Throw();
}

😄

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't help to make the method more inlinable. So this is just a matter of style.
(Btw, my take is that don't really like the new version, maybe my brain didn't re-wire itself to parse it) :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

One less line of code.

Doesn't necessarily make it faster to compile either.

😄

Interlocked.CompareExchange(ref target, value, null!);
Debug.Assert(target != null);
return target;

[DoesNotReturn]
static void Throw() => throw new InvalidOperationException(SR.Lazy_StaticInit_InvalidOperation);
}

/// <summary>
Expand Down
Loading