浏览代码

Merge pull request #1332 from dotnet/dev/bartde/modernize_some_more

Modernize some code.
Bart J.F. De Smet 5 年之前
父节点
当前提交
f37c27a557

+ 9 - 27
Rx.NET/Source/src/Microsoft.Reactive.Testing/Recorded.cs

@@ -19,18 +19,19 @@ namespace Microsoft.Reactive.Testing
 #endif
     public readonly struct Recorded<T> : IEquatable<Recorded<T>>
     {
+        // NB: Keep these fields for compat with serialized state.
         private readonly long _time;
         private readonly T _value;
 
         /// <summary>
         /// Gets the virtual time the value was produced on.
         /// </summary>
-        public long Time { get { return _time; } }
+        public long Time => _time;
 
         /// <summary>
         /// Gets the recorded value.
         /// </summary>
-        public T Value { get { return _value; } }
+        public T Value => _value;
 
         /// <summary>
         /// Creates a new object recording the production of the specified value at the given virtual time.
@@ -48,10 +49,7 @@ namespace Microsoft.Reactive.Testing
         /// </summary>
         /// <param name="other">Recorded object to check for equality.</param>
         /// <returns>true if both objects are equal; false otherwise.</returns>
-        public bool Equals(Recorded<T> other)
-        {
-            return Time == other.Time && EqualityComparer<T>.Default.Equals(Value, other.Value);
-        }
+        public bool Equals(Recorded<T> other) => Time == other.Time && EqualityComparer<T>.Default.Equals(Value, other.Value);
 
         /// <summary>
         /// Determines whether the two specified <see cref="Recorded{T}"/> values have the same Time and Value.
@@ -59,10 +57,7 @@ namespace Microsoft.Reactive.Testing
         /// <param name="left">The first <see cref="Recorded{T}"/> value to compare.</param>
         /// <param name="right">The second <see cref="Recorded{T}"/> value to compare.</param>
         /// <returns>true if the first <see cref="Recorded{T}"/> value has the same Time and Value as the second <see cref="Recorded{T}"/> value; otherwise, false.</returns>
-        public static bool operator ==(Recorded<T> left, Recorded<T> right)
-        {
-            return left.Equals(right);
-        }
+        public static bool operator ==(Recorded<T> left, Recorded<T> right) => left.Equals(right);
 
         /// <summary>
         /// Determines whether the two specified <see cref="Recorded{T}"/> values don't have the same Time and Value.
@@ -70,25 +65,14 @@ namespace Microsoft.Reactive.Testing
         /// <param name="left">The first <see cref="Recorded{T}"/> value to compare.</param>
         /// <param name="right">The second <see cref="Recorded{T}"/> value to compare.</param>
         /// <returns>true if the first <see cref="Recorded{T}"/> value has a different Time or Value as the second <see cref="Recorded{T}"/> value; otherwise, false.</returns>
-        public static bool operator !=(Recorded<T> left, Recorded<T> right)
-        {
-            return !left.Equals(right);
-        }
+        public static bool operator !=(Recorded<T> left, Recorded<T> right) => !left.Equals(right);
 
         /// <summary>
         /// Determines whether the specified System.Object is equal to the current <see cref="Recorded{T}"/> value.
         /// </summary>
         /// <param name="obj">The System.Object to compare with the current <see cref="Recorded{T}"/> value.</param>
         /// <returns>true if the specified System.Object is equal to the current <see cref="Recorded{T}"/> value; otherwise, false.</returns>
-        public override bool Equals(object obj)
-        {
-            if (obj is Recorded<T>)
-            {
-                return Equals((Recorded<T>)obj);
-            }
-
-            return false;
-        }
+        public override bool Equals(object obj) => obj is Recorded<T> && Equals((Recorded<T>)obj);
 
         /// <summary>
         /// Returns the hash code for the current <see cref="Recorded{T}"/> value.
@@ -96,6 +80,7 @@ namespace Microsoft.Reactive.Testing
         /// <returns>A hash code for the current <see cref="Recorded{T}"/> value.</returns>
         public override int GetHashCode()
         {
+            // TODO: Use proper hash code combiner.
             return Time.GetHashCode() + EqualityComparer<T>.Default.GetHashCode(Value);
         }
 
@@ -103,9 +88,6 @@ namespace Microsoft.Reactive.Testing
         /// Returns a string representation of the current <see cref="Recorded{T}"/> value.
         /// </summary>
         /// <returns>String representation of the current <see cref="Recorded{T}"/> value.</returns>
-        public override string ToString()
-        {
-            return Value.ToString() + "@" + Time.ToString(CultureInfo.CurrentCulture);
-        }
+        public override string ToString() => Value.ToString() + "@" + Time.ToString(CultureInfo.CurrentCulture);
     }
 }

+ 8 - 23
Rx.NET/Source/src/Microsoft.Reactive.Testing/Subscription.cs

@@ -22,18 +22,19 @@ namespace Microsoft.Reactive.Testing
         /// </summary>
         public const long Infinite = long.MaxValue;
 
+        // NB: Keep these fields for compat with serialized state.
         private readonly long _subscribe;
         private readonly long _unsubscribe;
 
         /// <summary>
         /// Gets the subscription virtual time.
         /// </summary>
-        public long Subscribe { get { return _subscribe; } }
+        public long Subscribe => _subscribe;
 
         /// <summary>
         /// Gets the unsubscription virtual time.
         /// </summary>
-        public long Unsubscribe { get { return _unsubscribe; } }
+        public long Unsubscribe => _unsubscribe;
 
         /// <summary>
         /// Creates a new subscription object with the given virtual subscription time.
@@ -61,10 +62,7 @@ namespace Microsoft.Reactive.Testing
         /// </summary>
         /// <param name="other">Subscription object to check for equality.</param>
         /// <returns>true if both objects are equal; false otherwise.</returns>
-        public bool Equals(Subscription other)
-        {
-            return Subscribe == other.Subscribe && Unsubscribe == other.Unsubscribe;
-        }
+        public bool Equals(Subscription other) => Subscribe == other.Subscribe && Unsubscribe == other.Unsubscribe;
 
         /// <summary>
         /// Determines whether the two specified Subscription values have the same Subscribe and Unsubscribe.
@@ -72,10 +70,7 @@ namespace Microsoft.Reactive.Testing
         /// <param name="left">The first Subscription value to compare.</param>
         /// <param name="right">The second Subscription value to compare.</param>
         /// <returns>true if the first Subscription value has the same Subscribe and Unsubscribe as the second Subscription value; otherwise, false.</returns>
-        public static bool operator ==(Subscription left, Subscription right)
-        {
-            return left.Equals(right);
-        }
+        public static bool operator ==(Subscription left, Subscription right) => left.Equals(right);
 
         /// <summary>
         /// Determines whether the two specified Subscription values don't have the same Subscribe and Unsubscribe.
@@ -83,25 +78,14 @@ namespace Microsoft.Reactive.Testing
         /// <param name="left">The first Subscription value to compare.</param>
         /// <param name="right">The second Subscription value to compare.</param>
         /// <returns>true if the first Subscription value has a different Subscribe or Unsubscribe as the second Subscription value; otherwise, false.</returns>
-        public static bool operator !=(Subscription left, Subscription right)
-        {
-            return !left.Equals(right);
-        }
+        public static bool operator !=(Subscription left, Subscription right) => !left.Equals(right);
 
         /// <summary>
         /// Determines whether the specified System.Object is equal to the current Subscription value.
         /// </summary>
         /// <param name="obj">The System.Object to compare with the current Subscription value.</param>
         /// <returns>true if the specified System.Object is equal to the current Subscription value; otherwise, false.</returns>
-        public override bool Equals(object obj)
-        {
-            if (obj is Subscription)
-            {
-                return Equals((Subscription)obj);
-            }
-
-            return false;
-        }
+        public override bool Equals(object obj) => obj is Subscription && Equals((Subscription)obj);
 
         /// <summary>
         /// Returns the hash code for the current Subscription value.
@@ -109,6 +93,7 @@ namespace Microsoft.Reactive.Testing
         /// <returns>A hash code for the current Subscription value.</returns>
         public override int GetHashCode()
         {
+            // TODO: Use proper hash code combiner.
             return Subscribe.GetHashCode() ^ Unsubscribe.GetHashCode();
         }
 

+ 1 - 4
Rx.NET/Source/src/System.Reactive/Concurrency/ScheduledItem.cs

@@ -153,10 +153,7 @@ namespace System.Reactive.Concurrency
         /// </summary>
         public bool IsCanceled => Disposable.GetIsDisposed(ref _disposable);
 
-        void IDisposable.Dispose()
-        {
-            Cancel();
-        }
+        void IDisposable.Dispose() => Cancel();
     }
 
     /// <summary>

+ 6 - 26
Rx.NET/Source/src/System.Reactive/TimeInterval.cs

@@ -43,10 +43,7 @@ namespace System.Reactive
         /// </summary>
         /// <param name="other">An object to compare to the current <see cref="TimeInterval{T}"/> value.</param>
         /// <returns><c>true</c> if both <see cref="TimeInterval{T}"/> values have the same <see cref="Value"/> and <see cref="Interval"/>; otherwise, <c>false</c>.</returns>
-        public bool Equals(TimeInterval<T> other)
-        {
-            return other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
-        }
+        public bool Equals(TimeInterval<T> other) => other.Interval.Equals(Interval) && EqualityComparer<T>.Default.Equals(Value, other.Value);
 
         /// <summary>
         /// Determines whether the two specified <see cref="TimeInterval{T}"/> values have the same <see cref="Value"/> and <see cref="Interval"/>.
@@ -54,10 +51,7 @@ namespace System.Reactive
         /// <param name="first">The first <see cref="TimeInterval{T}"/> value to compare.</param>
         /// <param name="second">The second <see cref="TimeInterval{T}"/> value to compare.</param>
         /// <returns><c>true</c> if the first <see cref="TimeInterval{T}"/> value has the same <see cref="Value"/> and <see cref="Interval"/> as the second <see cref="TimeInterval{T}"/> value; otherwise, <c>false</c>.</returns>
-        public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second)
-        {
-            return first.Equals(second);
-        }
+        public static bool operator ==(TimeInterval<T> first, TimeInterval<T> second) => first.Equals(second);
 
         /// <summary>
         /// Determines whether the two specified <see cref="TimeInterval{T}"/> values don't have the same <see cref="Value"/> and <see cref="Interval"/>.
@@ -65,26 +59,14 @@ namespace System.Reactive
         /// <param name="first">The first <see cref="TimeInterval{T}"/> value to compare.</param>
         /// <param name="second">The second <see cref="TimeInterval{T}"/> value to compare.</param>
         /// <returns><c>true</c> if the first <see cref="TimeInterval{T}"/> value has a different <see cref="Value"/> or <see cref="Interval"/> as the second <see cref="TimeInterval{T}"/> value; otherwise, <c>false</c>.</returns>
-        public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second)
-        {
-            return !first.Equals(second);
-        }
+        public static bool operator !=(TimeInterval<T> first, TimeInterval<T> second) => !first.Equals(second);
 
         /// <summary>
         /// Determines whether the specified System.Object is equal to the current <see cref="TimeInterval{T}"/>.
         /// </summary>
         /// <param name="obj">The System.Object to compare with the current <see cref="TimeInterval{T}"/>.</param>
         /// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="TimeInterval{T}"/>; otherwise, <c>false</c>.</returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is TimeInterval<T>))
-            {
-                return false;
-            }
-
-            var other = (TimeInterval<T>)obj;
-            return Equals(other);
-        }
+        public override bool Equals(object obj) => obj is TimeInterval<T> && Equals((TimeInterval<T>)obj);
 
         /// <summary>
         /// Returns the hash code for the current <see cref="TimeInterval{T}"/> value.
@@ -92,6 +74,7 @@ namespace System.Reactive
         /// <returns>A hash code for the current <see cref="TimeInterval{T}"/> value.</returns>
         public override int GetHashCode()
         {
+            // TODO: Use proper hash code combiner.
             return Interval.GetHashCode() ^ (Value?.GetHashCode() ?? 1963);
         }
 
@@ -99,9 +82,6 @@ namespace System.Reactive
         /// Returns a string representation of the current <see cref="TimeInterval{T}"/> value.
         /// </summary>
         /// <returns>String representation of the current <see cref="TimeInterval{T}"/> value.</returns>
-        public override string ToString()
-        {
-            return string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
-        }
+        public override string ToString() => string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Interval);
     }
 }

+ 6 - 26
Rx.NET/Source/src/System.Reactive/Timestamped.cs

@@ -44,10 +44,7 @@ namespace System.Reactive
         /// </summary>
         /// <param name="other">An object to compare to the current <see cref="Timestamped{T}" /> value.</param>
         /// <returns><c>true</c> if both <see cref="Timestamped{T}" /> values have the same <see cref="Value"/> and <see cref="Timestamp"/>; otherwise, <c>false</c>.</returns>
-        public bool Equals(Timestamped<T> other)
-        {
-            return other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value);
-        }
+        public bool Equals(Timestamped<T> other) => other.Timestamp.Equals(Timestamp) && EqualityComparer<T>.Default.Equals(Value, other.Value);
 
         /// <summary>
         /// Determines whether the two specified <see cref="Timestamped{T}" /> values have the same <see cref="Value"/> and <see cref="Timestamp"/>.
@@ -55,10 +52,7 @@ namespace System.Reactive
         /// <param name="first">The first <see cref="Timestamped{T}" /> value to compare.</param>
         /// <param name="second">The second <see cref="Timestamped{T}" /> value to compare.</param>
         /// <returns><c>true</c> if the first <see cref="Timestamped{T}" /> value has the same <see cref="Value"/> and <see cref="Timestamp"/> as the second <see cref="Timestamped{T}" /> value; otherwise, <c>false</c>.</returns>
-        public static bool operator ==(Timestamped<T> first, Timestamped<T> second)
-        {
-            return first.Equals(second);
-        }
+        public static bool operator ==(Timestamped<T> first, Timestamped<T> second) => first.Equals(second);
 
         /// <summary>
         /// Determines whether the two specified <see cref="Timestamped{T}" /> values don't have the same <see cref="Value"/> and <see cref="Timestamp"/>.
@@ -66,26 +60,14 @@ namespace System.Reactive
         /// <param name="first">The first <see cref="Timestamped{T}" /> value to compare.</param>
         /// <param name="second">The second <see cref="Timestamped{T}" /> value to compare.</param>
         /// <returns><c>true</c> if the first <see cref="Timestamped{T}" /> value has a different <see cref="Value"/> or <see cref="Timestamp"/> as the second <see cref="Timestamped{T}" /> value; otherwise, <c>false</c>.</returns>
-        public static bool operator !=(Timestamped<T> first, Timestamped<T> second)
-        {
-            return !first.Equals(second);
-        }
+        public static bool operator !=(Timestamped<T> first, Timestamped<T> second) => !first.Equals(second);
 
         /// <summary>
         /// Determines whether the specified System.Object is equal to the current <see cref="Timestamped{T}" />.
         /// </summary>
         /// <param name="obj">The System.Object to compare with the current <see cref="Timestamped{T}" />.</param>
         /// <returns><c>true</c> if the specified System.Object is equal to the current <see cref="Timestamped{T}" />; otherwise, <c>false</c>.</returns>
-        public override bool Equals(object obj)
-        {
-            if (!(obj is Timestamped<T>))
-            {
-                return false;
-            }
-
-            var other = (Timestamped<T>)obj;
-            return Equals(other);
-        }
+        public override bool Equals(object obj) => obj is Timestamped<T> && Equals((Timestamped<T>)obj);
 
         /// <summary>
         /// Returns the hash code for the current <see cref="Timestamped{T}" /> value.
@@ -93,6 +75,7 @@ namespace System.Reactive
         /// <returns>A hash code for the current <see cref="Timestamped{T}" /> value.</returns>
         public override int GetHashCode()
         {
+            // TODO: Use proper hash code combiner.
             return Timestamp.GetHashCode() ^ (Value?.GetHashCode() ?? 1979);
         }
 
@@ -100,10 +83,7 @@ namespace System.Reactive
         /// Returns a string representation of the current <see cref="Timestamped{T}" /> value.
         /// </summary>
         /// <returns>String representation of the current <see cref="Timestamped{T}" /> value.</returns>
-        public override string ToString()
-        {
-            return string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
-        }
+        public override string ToString() => string.Format(CultureInfo.CurrentCulture, "{0}@{1}", Value, Timestamp);
     }
 
     /// <summary>