浏览代码

Merge pull request #712 from akarnokd/SimplifyDefaultExpr

4.x: simplify default(X) usages
Daniel C. Weber 7 年之前
父节点
当前提交
0d41a8876c

+ 15 - 4
Rx.NET/Source/src/System.Reactive/Linq/Observable/Aggregate.cs

@@ -29,8 +29,6 @@ namespace System.Reactive.Linq.ObservableImpl
                 : base(observer)
             {
                 _accumulator = accumulator;
-                _accumulation = default;
-                _hasAccumulation = false;
             }
 
             public override void OnNext(TSource value)
@@ -48,11 +46,18 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     catch (Exception exception)
                     {
+                        _accumulation = default;
                         ForwardOnError(exception);
                     }
                 }
             }
 
+            public override void OnError(Exception error)
+            {
+                _accumulation = default;
+                ForwardOnError(error);
+            }
+
             public override void OnCompleted()
             {
                 if (!_hasAccumulation)
@@ -61,7 +66,9 @@ namespace System.Reactive.Linq.ObservableImpl
                 }
                 else
                 {
-                    ForwardOnNext(_accumulation);
+                    var accumulation = _accumulation;
+                    _accumulation = default;
+                    ForwardOnNext(accumulation);
                     ForwardOnCompleted();
                 }
             }
@@ -105,18 +112,22 @@ namespace System.Reactive.Linq.ObservableImpl
                 }
                 catch (Exception exception)
                 {
+                    _accumulation = default;
                     ForwardOnError(exception);
                 }
             }
 
             public override void OnError(Exception error)
             {
+                _accumulation = default;
                 ForwardOnError(error);
             }
 
             public override void OnCompleted()
             {
-                ForwardOnNext(_accumulation);
+                var accumulation = _accumulation;
+                _accumulation = default;
+                ForwardOnNext(accumulation);
                 ForwardOnCompleted();
             }
         }

+ 20 - 7
Rx.NET/Source/src/System.Reactive/Linq/Observable/LastAsync.cs

@@ -27,8 +27,7 @@ namespace System.Reactive.Linq.ObservableImpl
                 public _(IObserver<TSource> observer)
                     : base(observer)
                 {
-                    _value = default;
-                    _seenValue = false;
+
                 }
 
                 public override void OnNext(TSource value)
@@ -37,6 +36,12 @@ namespace System.Reactive.Linq.ObservableImpl
                     _seenValue = true;
                 }
 
+                public override void OnError(Exception error)
+                {
+                    _value = default;
+                    ForwardOnError(error);
+                }
+
                 public override void OnCompleted()
                 {
                     if (!_seenValue)
@@ -45,7 +50,9 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     else
                     {
-                        ForwardOnNext(_value);
+                        var value = _value;
+                        _value = default;
+                        ForwardOnNext(value);
                         ForwardOnCompleted();
                     }
                 }
@@ -77,9 +84,6 @@ namespace System.Reactive.Linq.ObservableImpl
                     : base(observer)
                 {
                     _predicate = predicate;
-
-                    _value = default;
-                    _seenValue = false;
                 }
 
                 public override void OnNext(TSource value)
@@ -92,6 +96,7 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     catch (Exception ex)
                     {
+                        _value = default;
                         ForwardOnError(ex);
                         return;
                     }
@@ -103,6 +108,12 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                 }
 
+                public override void OnError(Exception error)
+                {
+                    _value = default;
+                    ForwardOnError(error);
+                }
+
                 public override void OnCompleted()
                 {
                     if (!_seenValue)
@@ -111,7 +122,9 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     else
                     {
-                        ForwardOnNext(_value);
+                        var value = _value;
+                        _value = default;
+                        ForwardOnNext(value);
                         ForwardOnCompleted();
                     }
                 }

+ 20 - 5
Rx.NET/Source/src/System.Reactive/Linq/Observable/LastOrDefaultAsync.cs

@@ -26,7 +26,7 @@ namespace System.Reactive.Linq.ObservableImpl
                 public _(IObserver<TSource> observer)
                     : base(observer)
                 {
-                    _value = default;
+
                 }
 
                 public override void OnNext(TSource value)
@@ -34,9 +34,17 @@ namespace System.Reactive.Linq.ObservableImpl
                     _value = value;
                 }
 
+                public override void OnError(Exception error)
+                {
+                    _value = default;
+                    ForwardOnError(error);
+                }
+
                 public override void OnCompleted()
                 {
-                    ForwardOnNext(_value);
+                    var value = _value;
+                    _value = default;
+                    ForwardOnNext(value);
                     ForwardOnCompleted();
                 }
             }
@@ -66,8 +74,6 @@ namespace System.Reactive.Linq.ObservableImpl
                     : base(observer)
                 {
                     _predicate = predicate;
-
-                    _value = default;
                 }
 
                 public override void OnNext(TSource value)
@@ -80,6 +86,7 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                     catch (Exception ex)
                     {
+                        _value = default;
                         ForwardOnError(ex);
                         return;
                     }
@@ -90,9 +97,17 @@ namespace System.Reactive.Linq.ObservableImpl
                     }
                 }
 
+                public override void OnError(Exception error)
+                {
+                    _value = default;
+                    ForwardOnError(error);
+                }
+
                 public override void OnCompleted()
                 {
-                    ForwardOnNext(_value);
+                    var value = _value;
+                    _value = default;
+                    ForwardOnNext(value);
                     ForwardOnCompleted();
                 }
             }

+ 1 - 0
Rx.NET/Source/tests/Tests.System.Reactive/Tests.System.Reactive.csproj

@@ -2,6 +2,7 @@
   <PropertyGroup>
     <TargetFrameworks>net46;netcoreapp2.0</TargetFrameworks>
     <NoWarn>$(NoWarn);CS0618</NoWarn>
+    <LangVersion>7.1</LangVersion>
   </PropertyGroup>
   
   <ItemGroup>

+ 1 - 1
Rx.NET/Source/tests/Tests.System.Reactive/Tests/Linq/Observable/UsingTest.cs

@@ -46,7 +46,7 @@ namespace ReactiveTests.Tests
                         _d = d;
                         createInvoked++;
                         xs = scheduler.CreateColdObservable(
-                            OnNext<long>(100, scheduler.Clock),
+                            OnNext(100, scheduler.Clock),
                             OnCompleted<long>(200));
                         return xs;
                     }