<PackageReference Include="System.Reactive" Version="5.0.0-preview.220" />
AverageInt32
using System.
Runtime.
CompilerServices;
namespace System.
Reactive.
Linq.
ObservableImpl
{
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
[
System.
Runtime.
CompilerServices.
Nullable(
new byte[] {
0,
1
})]
internal sealed class AverageInt32 :
Producer<
double,
AverageInt32.
_>
{
[
System.
Runtime.
CompilerServices.
NullableContext(
0)]
internal sealed class _ :
Sink<
int,
double>
{
private long _sum;
private long _count;
[
System.
Runtime.
CompilerServices.
NullableContext(
1)]
public _(
IObserver<
double>
observer)
:
base(
observer)
{
_sum =
0;
_count =
0;
}
public override void OnNext(
int value)
{
checked {
try {
_sum +=
value;
_count++;
}
catch (
Exception error) {
ForwardOnError(
error);
}
}
}
public override void OnCompleted()
{
if (
_count <=
0)
try {
throw new InvalidOperationException(
Strings_Linq.
NO_ELEMENTS);
}
catch (
Exception error) {
ForwardOnError(
error);
}
else {
ForwardOnNext((
double)
_sum / (
double)
_count);
ForwardOnCompleted();
}
}
}
private readonly IObservable<
int>
_source;
public AverageInt32(
IObservable<
int>
source)
{
_source =
source;
}
protected override _ CreateSink(
IObserver<
double>
observer)
{
return new _(
observer);
}
protected override void Run(
_ sink)
{
sink.
Run(
_source);
}
}
}