From 3fa3fb8b6a3a5b4ad436534856fe24120533be17 Mon Sep 17 00:00:00 2001 From: Bart De Smet Date: Wed, 19 Apr 2017 16:31:33 -0700 Subject: [PATCH] Using C# 7 pattern matching features. --- .../Concurrency/Scheduler.Services.cs | 3 +-- .../Source/src/System.Reactive/Internal/Helpers.cs | 9 +++------ .../src/System.Reactive/Internal/SafeObserver.cs | 3 +-- .../src/System.Reactive/Linq/Observable/Catch.cs | 6 ++---- .../Linq/Observable/OnErrorResumeNext.cs | 3 +-- Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs | 6 ++---- .../Source/src/System.Reactive/Linq/QbservableEx.cs | 6 ++---- .../src/System.Reactive/Linq/QueryLanguage.Single.cs | 9 +++------ .../Linq/QueryLanguage.StandardSequenceOperators.cs | 9 +++------ .../src/System.Reactive/Linq/QueryLanguage.Time.cs | 12 ++++-------- .../src/System.Reactive/Observable.Extensions.cs | 3 +-- Rx.NET/Source/src/System.Reactive/ObservableQuery.cs | 6 ++---- .../Source/src/System.Reactive/Subjects/Subject.cs | 9 +++------ 13 files changed, 28 insertions(+), 56 deletions(-) diff --git a/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Services.cs b/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Services.cs index 1ff021ec5a..4eb166069e 100644 --- a/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Services.cs +++ b/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Services.cs @@ -68,8 +68,7 @@ public static partial class Scheduler private static T As(IScheduler scheduler) where T : class { - var svc = scheduler as IServiceProvider; - if (svc != null) + if (scheduler is IServiceProvider svc) { return (T)svc.GetService(typeof(T)); } diff --git a/Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs b/Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs index 06bd0d41e7..5e86aac3e3 100644 --- a/Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs +++ b/Rx.NET/Source/src/System.Reactive/Internal/Helpers.cs @@ -10,12 +10,10 @@ internal static class Helpers { public static int? GetLength(IEnumerable source) { - var array = source as T[]; - if (array != null) + if (source is T[] array) return array.Length; - var list = source as IList; - if (list != null) + if (source is IList list) return list.Count; return null; @@ -29,8 +27,7 @@ public static IObservable Unpack(IObservable source) { hasOpt = false; - var eval = source as IEvaluatableObservable; - if (eval != null) + if (source is IEvaluatableObservable eval) { source = eval.Eval(); hasOpt = true; diff --git a/Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs b/Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs index 4c5acf8806..5eec2774cf 100644 --- a/Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs +++ b/Rx.NET/Source/src/System.Reactive/Internal/SafeObserver.cs @@ -16,8 +16,7 @@ internal sealed class SafeObserver : IObserver public static IObserver Create(IObserver observer, IDisposable disposable) { - var a = observer as AnonymousObserver; - if (a != null) + if (observer is AnonymousObserver a) { return a.MakeSafe(disposable); } diff --git a/Rx.NET/Source/src/System.Reactive/Linq/Observable/Catch.cs b/Rx.NET/Source/src/System.Reactive/Linq/Observable/Catch.cs index 2b7e9b58c0..271d1cee58 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/Observable/Catch.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/Observable/Catch.cs @@ -32,8 +32,7 @@ public _(IObserver observer, IDisposable cancel) protected override IEnumerable> Extract(IObservable source) { - var @catch = source as Catch; - if (@catch != null) + if (source is Catch @catch) return @catch._sources; return null; @@ -129,8 +128,7 @@ public void OnNext(TSource value) public void OnError(Exception error) { - var e = error as TException; - if (e != null) + if (error is TException e) { var result = default(IObservable); try diff --git a/Rx.NET/Source/src/System.Reactive/Linq/Observable/OnErrorResumeNext.cs b/Rx.NET/Source/src/System.Reactive/Linq/Observable/OnErrorResumeNext.cs index 052faf13b0..52325a525a 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/Observable/OnErrorResumeNext.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/Observable/OnErrorResumeNext.cs @@ -31,8 +31,7 @@ public _(IObserver observer, IDisposable cancel) protected override IEnumerable> Extract(IObservable source) { - var oern = source as OnErrorResumeNext; - if (oern != null) + if (source is OnErrorResumeNext oern) return oern._sources; return null; diff --git a/Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs b/Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs index e928df259e..5eb2776221 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/Qbservable.cs @@ -91,8 +91,7 @@ public static IQbservable ToQbservable(this IQueryable(IObservable source) { - var q = source as IQbservable; - if (q != null) + if (source is IQbservable q) return q.Expression; return Expression.Constant(source, typeof(IObservable)); @@ -100,8 +99,7 @@ internal static Expression GetSourceExpression(IObservable sou internal static Expression GetSourceExpression(IEnumerable source) { - var q = source as IQueryable; - if (q != null) + if (source is IQueryable q) return q.Expression; return Expression.Constant(source, typeof(IEnumerable)); diff --git a/Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.cs b/Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.cs index 047011a3a2..0654bf9622 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/QbservableEx.cs @@ -17,8 +17,7 @@ public static partial class QbservableEx { internal static Expression GetSourceExpression(IObservable source) { - var q = source as IQbservable; - if (q != null) + if (source is IQbservable q) return q.Expression; return Expression.Constant(source, typeof(IObservable)); @@ -26,8 +25,7 @@ internal static Expression GetSourceExpression(IObservable sou internal static Expression GetSourceExpression(IEnumerable source) { - var q = source as IQueryable; - if (q != null) + if (source is IQueryable q) return q.Expression; return Expression.Constant(source, typeof(IEnumerable)); diff --git a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Single.cs b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Single.cs index 3a4292b25a..562d622d57 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Single.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Single.cs @@ -16,8 +16,7 @@ internal partial class QueryLanguage public virtual IObservable AsObservable(IObservable source) { - var asObservable = source as AsObservable; - if (asObservable != null) + if (source is AsObservable asObservable) return asObservable; return new AsObservable(source); @@ -48,8 +47,7 @@ private static IObservable> Buffer_(IObservable public virtual IObservable Dematerialize(IObservable> source) { - var materialize = source as Materialize; - if (materialize != null) + if (source is Materialize materialize) return materialize.Dematerialize(); return new Dematerialize(source); @@ -133,8 +131,7 @@ public virtual IObservable Finally(IObservable source public virtual IObservable IgnoreElements(IObservable source) { - var ignoreElements = source as IgnoreElements; - if (ignoreElements != null) + if (source is IgnoreElements ignoreElements) return ignoreElements; return new IgnoreElements(source); diff --git a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.StandardSequenceOperators.cs b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.StandardSequenceOperators.cs index 74966aea79..b6a641db3f 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.StandardSequenceOperators.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.StandardSequenceOperators.cs @@ -344,8 +344,7 @@ private static IObservable SelectMany_(I public virtual IObservable Skip(IObservable source, int count) { - var skip = source as Skip.Count; - if (skip != null) + if (source is Skip.Count skip) return skip.Combine(count); return new Skip.Count(source, count); @@ -387,8 +386,7 @@ public virtual IObservable Take(IObservable source, i private static IObservable Take_(IObservable source, int count) { - var take = source as Take.Count; - if (take != null) + if (source is Take.Count take) return take.Combine(count); return new Take.Count(source, count); @@ -414,8 +412,7 @@ public virtual IObservable TakeWhile(IObservable sour public virtual IObservable Where(IObservable source, Func predicate) { - var where = source as Where.Predicate; - if (where != null) + if (source is Where.Predicate where) return where.Combine(predicate); return new Where.Predicate(source, predicate); diff --git a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Time.cs b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Time.cs index d0c674519e..3938b1d434 100644 --- a/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Time.cs +++ b/Rx.NET/Source/src/System.Reactive/Linq/QueryLanguage.Time.cs @@ -251,8 +251,7 @@ public virtual IObservable Skip(IObservable source, T private static IObservable Skip_(IObservable source, TimeSpan duration, IScheduler scheduler) { - var skip = source as Skip.Time; - if (skip != null && skip._scheduler == scheduler) + if (source is Skip.Time skip && skip._scheduler == scheduler) return skip.Combine(duration); return new Skip.Time(source, duration, scheduler); @@ -293,8 +292,7 @@ public virtual IObservable SkipUntil(IObservable sour private static IObservable SkipUntil_(IObservable source, DateTimeOffset startTime, IScheduler scheduler) { - var skipUntil = source as SkipUntil; - if (skipUntil != null && skipUntil._scheduler == scheduler) + if (source is SkipUntil skipUntil && skipUntil._scheduler == scheduler) return skipUntil.Combine(startTime); return new SkipUntil(source, startTime, scheduler); @@ -316,8 +314,7 @@ public virtual IObservable Take(IObservable source, T private static IObservable Take_(IObservable source, TimeSpan duration, IScheduler scheduler) { - var take = source as Take.Time; - if (take != null && take._scheduler == scheduler) + if (source is Take.Time take && take._scheduler == scheduler) return take.Combine(duration); return new Take.Time(source, duration, scheduler); @@ -378,8 +375,7 @@ public virtual IObservable TakeUntil(IObservable sour private static IObservable TakeUntil_(IObservable source, DateTimeOffset endTime, IScheduler scheduler) { - var takeUntil = source as TakeUntil; - if (takeUntil != null && takeUntil._scheduler == scheduler) + if (source is TakeUntil takeUntil && takeUntil._scheduler == scheduler) return takeUntil.Combine(endTime); return new TakeUntil(source, endTime, scheduler); diff --git a/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs b/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs index 186a0567ec..1303db53da 100644 --- a/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs +++ b/Rx.NET/Source/src/System.Reactive/Observable.Extensions.cs @@ -321,8 +321,7 @@ public static IDisposable SubscribeSafe(this IObservable source, IObserver return source.Subscribe(observer); } - var producer = source as IProducer; - if (producer != null) + if (source is IProducer producer) { return producer.SubscribeRaw(observer, enableSafeguard: false); } diff --git a/Rx.NET/Source/src/System.Reactive/ObservableQuery.cs b/Rx.NET/Source/src/System.Reactive/ObservableQuery.cs index d820541dbe..60ce027792 100644 --- a/Rx.NET/Source/src/System.Reactive/ObservableQuery.cs +++ b/Rx.NET/Source/src/System.Reactive/ObservableQuery.cs @@ -143,8 +143,7 @@ public IDisposable Subscribe(IObserver observer) public override string ToString() { - var c = _expression as ConstantExpression; - if (c != null && c.Value == this) + if (_expression is ConstantExpression c && c.Value == this) { if (_source != null) return _source.ToString(); @@ -159,8 +158,7 @@ class ObservableRewriter : ExpressionVisitor { protected override Expression VisitConstant(ConstantExpression/*!*/ node) { - var query = node.Value as ObservableQuery; - if (query != null) + if (node.Value is ObservableQuery query) { var source = query.Source; if (source != null) diff --git a/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs b/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs index 2b384f72f1..1dc6ba9e35 100644 --- a/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs +++ b/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs @@ -141,8 +141,7 @@ public override IDisposable Subscribe(IObserver observer) return Disposable.Empty; } - var done = oldObserver as DoneObserver; - if (done != null) + if (oldObserver is DoneObserver done) { observer.OnError(done.Exception); return Disposable.Empty; @@ -154,8 +153,7 @@ public override IDisposable Subscribe(IObserver observer) } else { - var obs = oldObserver as Observer; - if (obs != null) + if (oldObserver is Observer obs) { newObserver = obs.Add(observer); } @@ -203,8 +201,7 @@ private void Unsubscribe(IObserver observer) if (oldObserver == DisposedObserver.Instance || oldObserver is DoneObserver) return; - var obs = oldObserver as Observer; - if (obs != null) + if (oldObserver is Observer obs) { newObserver = obs.Remove(observer); }