diff --git a/src/Weakly/Builders/CachingDynamicDelegateBuilderDecorator.cs b/src/Weakly/Builders/CachingDynamicDelegateBuilderDecorator.cs index e5ce965..5c2255b 100644 --- a/src/Weakly/Builders/CachingDynamicDelegateBuilderDecorator.cs +++ b/src/Weakly/Builders/CachingDynamicDelegateBuilderDecorator.cs @@ -30,7 +30,7 @@ public CachingDynamicDelegateBuilderDecorator(IDynamicDelegateBuilder builder) public Func BuildDynamic(MethodInfo method) { var action = _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildDynamic(method); _cache.AddOrUpdate(method, action); return action; diff --git a/src/Weakly/Builders/CachingOpenActionBuilderDecorator.cs b/src/Weakly/Builders/CachingOpenActionBuilderDecorator.cs index 7534c09..46c81f8 100644 --- a/src/Weakly/Builders/CachingOpenActionBuilderDecorator.cs +++ b/src/Weakly/Builders/CachingOpenActionBuilderDecorator.cs @@ -30,7 +30,7 @@ public CachingOpenActionBuilderDecorator(IOpenActionBuilder builder) public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; @@ -47,7 +47,7 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; @@ -65,7 +65,7 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; @@ -84,7 +84,7 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; @@ -104,7 +104,7 @@ public Action BuildAction(MethodInfo method) public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; @@ -125,7 +125,7 @@ public Action BuildAction(MethodInfo met public Action BuildAction(MethodInfo method) { var action = (Action) _cache.GetValueOrDefault(method); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildAction(method); _cache.AddOrUpdate(method, action); return action; diff --git a/src/Weakly/Builders/CachingOpenFuncBuilderDecorator.cs b/src/Weakly/Builders/CachingOpenFuncBuilderDecorator.cs index c46a12e..27855f5 100644 --- a/src/Weakly/Builders/CachingOpenFuncBuilderDecorator.cs +++ b/src/Weakly/Builders/CachingOpenFuncBuilderDecorator.cs @@ -31,7 +31,7 @@ public CachingOpenFuncBuilderDecorator(IOpenFuncBuilder builder) public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; @@ -49,7 +49,7 @@ public Func BuildFunc(MethodInfo method) public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; @@ -68,7 +68,7 @@ public Func BuildFunc(MethodInfo method) public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; @@ -88,7 +88,7 @@ public Func BuildFunc(MethodInfo metho public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; @@ -109,7 +109,7 @@ public Func BuildFunc(MethodIn public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; @@ -131,7 +131,7 @@ public Func BuildFunc( public Func BuildFunc(MethodInfo method) { var func = (Func) _cache.GetValueOrDefault(method); - if (func != null) return func; + if (func is object) return func; func = _builder.BuildFunc(method); _cache.AddOrUpdate(method, func); return func; diff --git a/src/Weakly/Builders/CachingPropertyAccessorBuilderDecorator.cs b/src/Weakly/Builders/CachingPropertyAccessorBuilderDecorator.cs index f108d54..e4b4717 100644 --- a/src/Weakly/Builders/CachingPropertyAccessorBuilderDecorator.cs +++ b/src/Weakly/Builders/CachingPropertyAccessorBuilderDecorator.cs @@ -31,7 +31,7 @@ public CachingPropertyAccessorBuilderDecorator(IPropertyAccessorBuilder builder) public Func BuildGetter(PropertyInfo property) { var action = _getterCache.GetValueOrDefault(property); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildGetter(property); _getterCache.AddOrUpdate(property, action); return action; @@ -47,7 +47,7 @@ public Func BuildGetter(PropertyInfo property) public Action BuildSetter(PropertyInfo property) { var action = _setterCache.GetValueOrDefault(property); - if (action != null) return action; + if (action is object) return action; action = _builder.BuildSetter(property); _setterCache.AddOrUpdate(property, action); return action; diff --git a/src/Weakly/Builders/SimpleCache.cs b/src/Weakly/Builders/SimpleCache.cs index 60d5afd..4d026e1 100644 --- a/src/Weakly/Builders/SimpleCache.cs +++ b/src/Weakly/Builders/SimpleCache.cs @@ -4,12 +4,7 @@ namespace Weakly.Builders { internal sealed class SimpleCache { - private readonly IDictionary _storage; - - public SimpleCache() - { - _storage = new Dictionary(); - } + private readonly Dictionary _storage = new Dictionary(); public TValue GetValueOrDefault(TKey key) { diff --git a/src/Weakly/Collections/WeakCollection.cs b/src/Weakly/Collections/WeakCollection.cs index 39250ff..4977d4e 100644 --- a/src/Weakly/Collections/WeakCollection.cs +++ b/src/Weakly/Collections/WeakCollection.cs @@ -72,7 +72,7 @@ public IEnumerator GetEnumerator() CleanIfNeeded(); var enumerable = _inner.Select(item => (T) item.Target) - .Where(value => value != null); + .Where(value => value is object); return enumerable.GetEnumerator(); } @@ -119,7 +119,7 @@ public void CopyTo(T[] array, int arrayIndex) { CleanIfNeeded(); - if (array == null) + if (array is null) throw new ArgumentNullException(nameof(array)); if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); @@ -127,7 +127,7 @@ public void CopyTo(T[] array, int arrayIndex) throw new ArgumentException("The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array."); var items = _inner.Select(item => (T) item.Target) - .Where(value => value != null) + .Where(value => value is object) .ToArray(); items.CopyTo(array, arrayIndex); diff --git a/src/Weakly/Collections/WeakValueDictionary.cs b/src/Weakly/Collections/WeakValueDictionary.cs index 77646f8..a02924e 100644 --- a/src/Weakly/Collections/WeakValueDictionary.cs +++ b/src/Weakly/Collections/WeakValueDictionary.cs @@ -121,7 +121,7 @@ public IEnumerator> GetEnumerator() CleanIfNeeded(); var enumerable = _inner.Select(pair => new KeyValuePair(pair.Key, (TValue) pair.Value.Target)) - .Where(pair => pair.Value != null); + .Where(pair => pair.Value is object); return enumerable.GetEnumerator(); } @@ -156,7 +156,7 @@ void ICollection>.CopyTo(KeyValuePair[] { CleanIfNeeded(); - if (array == null) + if (array is null) throw new ArgumentNullException(nameof(array)); if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); @@ -164,7 +164,7 @@ void ICollection>.CopyTo(KeyValuePair[] throw new ArgumentException("The number of elements in the source collection is greater than the available space from arrayIndex to the end of the destination array."); var items = _inner.Select(pair => new KeyValuePair(pair.Key, (TValue) pair.Value.Target)) - .Where(pair => pair.Value != null) + .Where(pair => pair.Value is object) .ToArray(); items.CopyTo(array, arrayIndex); @@ -256,7 +256,7 @@ public bool TryGetValue(TKey key, out TValue value) } value = (TValue) wr.Target; - if (value == null) + if (value is null) { _inner.Remove(key); return false; @@ -343,7 +343,7 @@ bool ICollection.Contains(TValue item) public void CopyTo(TValue[] array, int arrayIndex) { - if (array == null) + if (array is null) throw new ArgumentNullException(nameof(array)); if (arrayIndex < 0 || arrayIndex >= array.Length) throw new ArgumentOutOfRangeException(nameof(arrayIndex)); diff --git a/src/Weakly/Delegates/DisposableAction.cs b/src/Weakly/Delegates/DisposableAction.cs index 734ed7e..12efaeb 100644 --- a/src/Weakly/Delegates/DisposableAction.cs +++ b/src/Weakly/Delegates/DisposableAction.cs @@ -15,7 +15,7 @@ public sealed class DisposableAction : IDisposable /// The action to execute on dispose. public DisposableAction(Action action) { - if (action == null) + if (action is null) throw new ArgumentNullException(nameof(action)); _action = action; @@ -26,7 +26,7 @@ public DisposableAction(Action action) /// public void Dispose() { - if (_action == null) return; + if (_action is null) return; _action(); _action = null; } diff --git a/src/Weakly/Delegates/WeakAction.cs b/src/Weakly/Delegates/WeakAction.cs index 86e093a..ea99a14 100644 --- a/src/Weakly/Delegates/WeakAction.cs +++ b/src/Weakly/Delegates/WeakAction.cs @@ -19,9 +19,9 @@ public sealed class WeakAction /// The method represented by the delegate. public WeakAction(TTarget target, [EmptyCapture] Action weakAction) { - if (target == null) + if (target is null) throw new ArgumentNullException(nameof(target)); - if (weakAction == null) + if (weakAction is null) throw new ArgumentNullException(nameof(weakAction)); _target = new WeakReference(target); diff --git a/src/Weakly/Delegates/WeakFunc.cs b/src/Weakly/Delegates/WeakFunc.cs index 4b9282d..eab8d8b 100644 --- a/src/Weakly/Delegates/WeakFunc.cs +++ b/src/Weakly/Delegates/WeakFunc.cs @@ -20,9 +20,9 @@ public sealed class WeakFunc /// The method represented by the delegate. public WeakFunc(TTarget target, [EmptyCapture] Func weakFunc) { - if (target == null) + if (target is null) throw new ArgumentNullException(nameof(target)); - if (weakFunc == null) + if (weakFunc is null) throw new ArgumentNullException(nameof(weakFunc)); _target = new WeakReference(target); diff --git a/src/Weakly/Events/WeakEventHandler.cs b/src/Weakly/Events/WeakEventHandler.cs index dc05685..2b41d4f 100644 --- a/src/Weakly/Events/WeakEventHandler.cs +++ b/src/Weakly/Events/WeakEventHandler.cs @@ -20,7 +20,7 @@ public static class WeakEventHandler /// The weak handler. /// A registration object that can be used to deregister from the event. public static IDisposable RegisterPropertyChangingWeak(this INotifyPropertyChanging source, - TSubscriber subscriber, Action weakHandler) + TSubscriber subscriber, [EmptyCapture] Action weakHandler) where TSubscriber : class { return new WeakNotifyPropertyChangingHandler(source, subscriber, weakHandler); diff --git a/src/Weakly/IO/MemoryTributary.cs b/src/Weakly/IO/MemoryTributary.cs index 75a307f..ef73f58 100644 --- a/src/Weakly/IO/MemoryTributary.cs +++ b/src/Weakly/IO/MemoryTributary.cs @@ -145,7 +145,7 @@ public override int Read(byte[] buffer, int offset, int count) if (lcount > remaining) lcount = remaining; - if (buffer == null) + if (buffer is null) { throw new ArgumentNullException(nameof(buffer), "Buffer cannot be null."); } diff --git a/src/Weakly/Reflection/ReflectionPath.cs b/src/Weakly/Reflection/ReflectionPath.cs index 6884538..caed8be 100644 --- a/src/Weakly/Reflection/ReflectionPath.cs +++ b/src/Weakly/Reflection/ReflectionPath.cs @@ -19,7 +19,7 @@ public sealed class ReflectionPath /// The reflection path. public ReflectionPath(string path) { - if (path == null) + if (path is null) throw new ArgumentNullException(nameof(path)); _items = path.Split('.'); @@ -34,13 +34,13 @@ public ReflectionPath(string path) /// The value. public object GetValue(object source) { - if (source == null) + if (source is null) throw new ArgumentNullException(nameof(source)); var current = source; for (var i = 0; i < _items.Length; i++) { - if (current == null) + if (current is null) throw new NullReferenceException( string.Format( "Could not get the value of the property path '{0}' because '{1}' is null on object '{2}'.", @@ -52,10 +52,10 @@ public object GetValue(object source) var getValue = _getters[i]; var currentType = current.GetType(); - if (getValue == null || _reflectedTypes[i] != currentType) + if (getValue is null || _reflectedTypes[i] != currentType) { var pi = currentType.GetRuntimeProperty(_items[i]); - if (pi == null) + if (pi is null) throw new InvalidOperationException( string.Format( "Could not find property '{0}' on type '{1}'.", diff --git a/src/Weakly/Tasks/ApmHelper.cs b/src/Weakly/Tasks/ApmHelper.cs index 78b4d05..f6cd66f 100644 --- a/src/Weakly/Tasks/ApmHelper.cs +++ b/src/Weakly/Tasks/ApmHelper.cs @@ -18,12 +18,12 @@ public static class ApmHelper /// The asynchronous operation, to be returned by the Begin method of the APM pattern. public static IAsyncResult ToBegin(this Task task, AsyncCallback callback, object state) { - if (task == null) + if (task is null) throw new ArgumentNullException(nameof(task)); if (task.AsyncState == state) { - if (callback != null) + if (callback is object) { task.ContinueWith( (t, cb) => ((AsyncCallback)cb)(t), @@ -53,7 +53,7 @@ public static IAsyncResult ToBegin(this Task task, AsyncCallba tcs.TrySetResult(t.Result); } - if (callback != null) + if (callback is object) { callback(tcs.Task); } @@ -74,12 +74,12 @@ public static IAsyncResult ToBegin(this Task task, AsyncCallba /// The asynchronous operation, to be returned by the Begin method of the APM pattern. public static IAsyncResult ToBegin(this Task task, AsyncCallback callback, object state) { - if (task == null) + if (task is null) throw new ArgumentNullException(nameof(task)); if (task.AsyncState == state) { - if (callback != null) + if (callback is object) { task.ContinueWith( (t, cb) => ((AsyncCallback)cb)(t), @@ -109,7 +109,7 @@ public static IAsyncResult ToBegin(this Task task, AsyncCallback callback, objec tcs.TrySetResult(null); } - if (callback != null) + if (callback is object) { callback(tcs.Task); } diff --git a/src/Weakly/Tasks/TaskEventArgs.cs b/src/Weakly/Tasks/TaskEventArgs.cs index 797237a..8999fed 100644 --- a/src/Weakly/Tasks/TaskEventArgs.cs +++ b/src/Weakly/Tasks/TaskEventArgs.cs @@ -16,7 +16,7 @@ public sealed class TaskEventArgs : EventArgs /// The supplied Task. public TaskEventArgs(Task task) { - if (task == null) + if (task is null) throw new ArgumentNullException(nameof(task)); _task = task; diff --git a/src/Weakly/Tasks/TaskHelper.cs b/src/Weakly/Tasks/TaskHelper.cs index a7a0935..a7b5de0 100644 --- a/src/Weakly/Tasks/TaskHelper.cs +++ b/src/Weakly/Tasks/TaskHelper.cs @@ -137,7 +137,7 @@ public static Task FailFastOnException(this Task task) private static void OnTaskFaulted(Task task) { var handler = TaskFaulted; - if (handler != null) + if (handler is object) handler(null, new TaskEventArgs(task)); } @@ -208,11 +208,11 @@ public static void PropagateExceptionsTo(this Task task, SynchronizationContext /// The task to watch. public static void Watch(this Task task) { - if (task == null) + if (task is null) throw new ArgumentNullException(nameof(task)); var handler = TaskWatched; - if (handler != null) + if (handler is object) handler(null, new TaskEventArgs(task)); }