Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 6 additions & 6 deletions src/System.Private.CoreLib/Common/System/SR.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static string GetResourceString(string resourceKey, string defaultStrin
try { resourceString = InternalGetResourceString(resourceKey); }
catch (MissingManifestResourceException) { }

if (defaultString != null && resourceKey.Equals(resourceString, StringComparison.Ordinal))
if (!(defaultString is null) && resourceKey.Equals(resourceString, StringComparison.Ordinal))
{
return defaultString;
}
Expand All @@ -54,7 +54,7 @@ internal static string GetResourceString(string resourceKey, string defaultStrin

private static string InternalGetResourceString(string key)
{
if (key == null || key.Length == 0)
if (key is null || key.Length == 0)
{
Debug.Fail("SR::GetResourceString with null or empty key. Bug in caller, or weird recursive loading problem?");
return key;
Expand Down Expand Up @@ -86,7 +86,7 @@ private static string InternalGetResourceString(string key)

// Are we recursively looking up the same resource? Note - our backout code will set
// the ResourceHelper's currentlyLoading stack to null if an exception occurs.
if (_currentlyLoading != null && _currentlyLoading.Count > 0 && _currentlyLoading.LastIndexOf(key) != -1)
if (!(_currentlyLoading is null) && _currentlyLoading.Count > 0 && _currentlyLoading.LastIndexOf(key) != -1)
{
// We can start infinitely recursing for one resource lookup,
// then during our failure reporting, start infinitely recursing again.
Expand All @@ -102,7 +102,7 @@ private static string InternalGetResourceString(string key)
string message = $"Infinite recursion during resource lookup within {System.CoreLib.Name}. This may be a bug in {System.CoreLib.Name}, or potentially in certain extensibility points such as assembly resolve events or CultureInfo names. Resource name: {key}";
Environment.FailFast(message);
}
if (_currentlyLoading == null)
if (_currentlyLoading is null)
_currentlyLoading = new List<string>();

// Call class constructors preemptively, so that we cannot get into an infinite
Expand All @@ -120,7 +120,7 @@ private static string InternalGetResourceString(string key)

_currentlyLoading.Add(key); // Push

if (ResourceManager == null)
if (ResourceManager is null)
Copy link
Member

Choose a reason for hiding this comment

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

Is here actually a difference as Roslyn generates the same code for both?

Though I find this version more readable / nicer 😉.

Copy link
Member Author

Choose a reason for hiding this comment

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

Can either pick consistency or ease of changing 4k+ call sites 😄

Early commits I was only picking a specific set of classes; doing it for the all == null and != null checks is either much slower or more broad brush (or would need time to write a tool)

Copy link
Member

Choose a reason for hiding this comment

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

That's right 😉

!(obj is null) vs. obj != null the latter is more readable.
obj is null vs obj == null I find both very readable.
So it's hard to find a good balance between consistency and readability, as for MethodInfo and the like the is-variant has to be taken perf-wise -- or adapt the compiler(s) to emit efficient code for the null-comparison.

Copy link
Member Author

@benaadams benaadams Jan 2, 2019

Choose a reason for hiding this comment

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

Was a discussion (on twitter) about a less fugly is not null check for C#; most popular variants seemed to be x !is null and x is !null

Don't know if there is a formal C# proposal though

Copy link
Member Author

Choose a reason for hiding this comment

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

VB's leading the way here by already having IsNot Nothing 😉

Copy link
Member

Choose a reason for hiding this comment

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

Too many channels to follow 😃 Thanks for the link!

Copy link
Member

Choose a reason for hiding this comment

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

formal proposal

It's there: dotnet/csharplang#27. Someone suggested aint operator as well. 😄

Choose a reason for hiding this comment

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

I think this is the current proposal that would end up landing the 'not' pattern: dotnet/csharplang#1350

Copy link

@Joe4evr Joe4evr Jan 2, 2019

Choose a reason for hiding this comment

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

For checking not null specifically, there's also the syntax that falls out from Property Patterns: if (foo is {}), though I will concede that it's not quite as readable as if (foo is not null), but the outcome is the exact same.

Copy link
Member Author

Choose a reason for hiding this comment

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

if (foo is object) also works, but undid that in e4389b0 as it wasn't very clear

{
ResourceManager = new ResourceManager(SR.ResourceType);
}
Expand Down Expand Up @@ -151,7 +151,7 @@ private static string InternalGetResourceString(string key)

internal static string Format(string resourceFormat, params object[] args)
{
if (args != null)
if (!(args is null))
{
if (UsingResourceKeys())
{
Expand Down
2 changes: 1 addition & 1 deletion src/System.Private.CoreLib/shared/Internal/IO/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static bool Exists(string path)
{
try
{
if (path == null)
if (path is null)
Copy link
Member

Choose a reason for hiding this comment

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

Is here actually a difference as Roslyn generates the same code for both?

BTW: here I would use string.IsNullOrEmpty for this and the following check.

return false;
if (path.Length == 0)
return false;
Expand Down
12 changes: 6 additions & 6 deletions src/System.Private.CoreLib/shared/Internal/Win32/RegistryKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private RegistryKey(SafeRegistryHandle hkey)

void IDisposable.Dispose()
{
if (_hkey != null)
if (!(_hkey is null))
{
_hkey.Dispose();
}
Expand Down Expand Up @@ -207,7 +207,7 @@ public unsafe string[] GetValueNames()
}
finally
{
if (name != null)
if (!(name is null))
ArrayPool<char>.Shared.Return(name);
}

Expand Down Expand Up @@ -410,7 +410,7 @@ public object GetValue(string name, object defaultValue)
}
cur = nextNull + 1;

if (toAdd != null)
if (!(toAdd is null))
{
if (strings.Length == stringsCount)
{
Expand All @@ -436,10 +436,10 @@ public object GetValue(string name, object defaultValue)
// so this is a cut-down version that supports on that.
internal void SetValue(string name, string value)
{
if (value == null)
if (value is null)
throw new ArgumentNullException(nameof(value));

if (name != null && name.Length > MaxValueLength)
if (!(name is null) && name.Length > MaxValueLength)
throw new ArgumentException(SR.Arg_RegValStrLenBug, nameof(name));

int ret = Interop.Advapi32.RegSetValueEx(_hkey,
Expand All @@ -460,7 +460,7 @@ internal void Win32Error(int errorCode, string str)
switch (errorCode)
{
case Interop.Errors.ERROR_ACCESS_DENIED:
if (str != null)
if (!(str is null))
throw new UnauthorizedAccessException(SR.Format(SR.UnauthorizedAccess_RegistryKeyGeneric_Key, str));
else
throw new UnauthorizedAccessException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ private static void ThrowExceptionForIoErrno(ErrorInfo errorInfo, string path, b
Debug.Assert(errorInfo.Error != Error.SUCCESS);
Debug.Assert(errorInfo.Error != Error.EINTR, "EINTR errors should be handled by the native shim and never bubble up to managed code");

if (errorRewriter != null)
if (!(errorRewriter is null))
{
errorInfo = errorRewriter(errorInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static unsafe string GetCwd()
// First try to get the path into a buffer on the stack
byte* stackBuf = stackalloc byte[StackLimit];
string result = GetCwdHelper(stackBuf, StackLimit);
if (result != null)
if (!(result is null))
{
return result;
}
Expand All @@ -36,7 +36,7 @@ internal static unsafe string GetCwd()
fixed (byte* ptr = &buf[0])
{
result = GetCwdHelper(ptr, buf.Length);
if (result != null)
if (!(result is null))
{
return result;
}
Expand Down
32 changes: 16 additions & 16 deletions src/System.Private.CoreLib/shared/System/AggregateException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public AggregateException(string message)
public AggregateException(string message, Exception innerException)
: base(message, innerException)
{
if (innerException == null)
if (innerException is null)
{
throw new ArgumentNullException(nameof(innerException));
}
Expand Down Expand Up @@ -107,7 +107,7 @@ public AggregateException(params Exception[] innerExceptions) :
public AggregateException(string message, IEnumerable<Exception> innerExceptions)
// If it's already an IList, pass that along (a defensive copy will be made in the delegated ctor). If it's null, just pass along
// null typed correctly. Otherwise, create an IList from the enumerable and pass that along.
: this(message, innerExceptions as IList<Exception> ?? (innerExceptions == null ? (List<Exception>)null : new List<Exception>(innerExceptions)))
: this(message, innerExceptions as IList<Exception> ?? (innerExceptions is null ? (List<Exception>)null : new List<Exception>(innerExceptions)))
{
}

Expand Down Expand Up @@ -136,9 +136,9 @@ public AggregateException(string message, params Exception[] innerExceptions) :
/// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptions"/> is
/// null.</exception>
private AggregateException(string message, IList<Exception> innerExceptions)
: base(message, innerExceptions != null && innerExceptions.Count > 0 ? innerExceptions[0] : null)
: base(message, !(innerExceptions is null) && innerExceptions.Count > 0 ? innerExceptions[0] : null)
{
if (innerExceptions == null)
if (innerExceptions is null)
{
throw new ArgumentNullException(nameof(innerExceptions));
}
Expand Down Expand Up @@ -194,7 +194,7 @@ internal AggregateException(string message, IEnumerable<ExceptionDispatchInfo> i
// If it's already an IList, pass that along (a defensive copy will be made in the delegated ctor). If it's null, just pass along
// null typed correctly. Otherwise, create an IList from the enumerable and pass that along.
: this(message, innerExceptionInfos as IList<ExceptionDispatchInfo> ??
(innerExceptionInfos == null ?
(innerExceptionInfos is null ?
(List<ExceptionDispatchInfo>)null :
new List<ExceptionDispatchInfo>(innerExceptionInfos)))
{
Expand All @@ -213,10 +213,10 @@ internal AggregateException(string message, IEnumerable<ExceptionDispatchInfo> i
/// <exception cref="T:System.ArgumentException">An element of <paramref name="innerExceptionInfos"/> is
/// null.</exception>
private AggregateException(string message, IList<ExceptionDispatchInfo> innerExceptionInfos)
: base(message, innerExceptionInfos != null && innerExceptionInfos.Count > 0 && innerExceptionInfos[0] != null ?
: base(message, !(innerExceptionInfos is null) && innerExceptionInfos.Count > 0 && innerExceptionInfos[0] != null ?
innerExceptionInfos[0].SourceException : null)
{
if (innerExceptionInfos == null)
if (innerExceptionInfos is null)
{
throw new ArgumentNullException(nameof(innerExceptionInfos));
}
Expand All @@ -229,7 +229,7 @@ private AggregateException(string message, IList<ExceptionDispatchInfo> innerExc
for (int i = 0; i < exceptionsCopy.Length; i++)
{
var edi = innerExceptionInfos[i];
if (edi != null) exceptionsCopy[i] = edi.SourceException;
if (!(edi is null)) exceptionsCopy[i] = edi.SourceException;

if (exceptionsCopy[i] == null)
{
Expand All @@ -252,13 +252,13 @@ private AggregateException(string message, IList<ExceptionDispatchInfo> innerExc
protected AggregateException(SerializationInfo info, StreamingContext context) :
base(info, context)
{
if (info == null)
if (info is null)
{
throw new ArgumentNullException(nameof(info));
}

Exception[] innerExceptions = info.GetValue("InnerExceptions", typeof(Exception[])) as Exception[];
if (innerExceptions == null)
if (innerExceptions is null)
{
throw new SerializationException(SR.AggregateException_DeserializationFailure);
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public override Exception GetBaseException()
// Recursively traverse the inner exceptions as long as the inner exception of type AggregateException and has only one inner exception
Exception back = this;
AggregateException backAsAggregate = this;
while (backAsAggregate != null && backAsAggregate.InnerExceptions.Count == 1)
while (!(backAsAggregate is null) && backAsAggregate.InnerExceptions.Count == 1)
{
back = back.InnerException;
backAsAggregate = back as AggregateException;
Expand Down Expand Up @@ -333,7 +333,7 @@ public ReadOnlyCollection<Exception> InnerExceptions
/// null.</exception>
public void Handle(Func<Exception, bool> predicate)
{
if (predicate == null)
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
Expand All @@ -345,7 +345,7 @@ public void Handle(Func<Exception, bool> predicate)
// exceptions (to be rethrown later) and add it.
if (!predicate(m_innerExceptions[i]))
{
if (unhandledExceptions == null)
if (unhandledExceptions is null)
{
unhandledExceptions = new List<Exception>();
}
Expand All @@ -355,7 +355,7 @@ public void Handle(Func<Exception, bool> predicate)
}

// If there are unhandled exceptions remaining, throw them.
if (unhandledExceptions != null)
if (!(unhandledExceptions is null))
{
throw new AggregateException(Message, unhandledExceptions);
}
Expand Down Expand Up @@ -394,7 +394,7 @@ public AggregateException Flatten()
{
Exception currentInnerException = currentInnerExceptions[i];

if (currentInnerException == null)
if (currentInnerException is null)
{
continue;
}
Expand All @@ -403,7 +403,7 @@ public AggregateException Flatten()

// If this exception is an aggregate, keep it around for later. Otherwise,
// simply add it to the list of flattened exceptions to be returned.
if (currentInnerAsAggregate != null)
if (!(currentInnerAsAggregate is null))
{
exceptionsToFlatten.Add(currentInnerAsAggregate);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public override string Message
get
{
string s = base.Message;
if (_actualValue != null)
if (!(_actualValue is null))
{
string valueMessage = SR.Format(SR.ArgumentOutOfRange_ActualValue, _actualValue.ToString());
if (s == null)
if (s is null)
return valueMessage;
return s + Environment.NewLine + valueMessage;
}
Expand Down
10 changes: 5 additions & 5 deletions src/System.Private.CoreLib/shared/System/ArraySegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace System

public ArraySegment(T[] array)
{
if (array == null)
if (array is null)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);

_array = array;
Expand All @@ -51,7 +51,7 @@ public ArraySegment(T[] array, int offset, int count)
// Validate arguments, check is minimal instructions with reduced branching for inlinable fast-path
// Negative values discovered though conversion to high values when converted to unsigned
// Failure should be rare and location determination and message is delegated to failure functions
if (array == null || (uint)offset > (uint)array.Length || (uint)count > (uint)(array.Length - offset))
if (array is null || (uint)offset > (uint)array.Length || (uint)count > (uint)(array.Length - offset))
ThrowHelper.ThrowArraySegmentCtorValidationFailedExceptions(array, offset, count);

_array = array;
Expand Down Expand Up @@ -95,7 +95,7 @@ public Enumerator GetEnumerator()

public override int GetHashCode()
{
if (_array == null)
if (_array is null)
{
return 0;
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public T[] ToArray()
return !(a == b);
}

public static implicit operator ArraySegment<T>(T[] array) => array != null ? new ArraySegment<T>(array) : default;
public static implicit operator ArraySegment<T>(T[] array) => !(array is null) ? new ArraySegment<T>(array) : default;

#region IList<T>
T IList<T>.this[int index]
Expand Down Expand Up @@ -305,7 +305,7 @@ bool ICollection<T>.Remove(T item)

private void ThrowInvalidOperationIfDefault()
{
if (_array == null)
if (_array is null)
{
ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_NullArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public override string Message

private void SetMessageField()
{
if (_message == null)
if (_message is null)
{
if ((_fileName == null) &&
if ((_fileName is null) &&
(HResult == HResults.COR_E_EXCEPTION))
_message = SR.Arg_BadImageFormatException;

Expand All @@ -100,18 +100,18 @@ public override string ToString()
{
string s = GetType().ToString() + ": " + Message;

if (_fileName != null && _fileName.Length != 0)
if (!(_fileName is null) && _fileName.Length != 0)
s += Environment.NewLine + SR.Format(SR.IO_FileName_Name, _fileName);

if (InnerException != null)
if (!(InnerException is null))
s = s + " ---> " + InnerException.ToString();

if (StackTrace != null)
if (!(StackTrace is null))
s += Environment.NewLine + StackTrace;

if (_fusionLog != null)
if (!(_fusionLog is null))
{
if (s == null)
if (s is null)
s = " ";
s += Environment.NewLine;
s += Environment.NewLine;
Expand Down
Loading