Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:54:12+00:00 2026-06-13T09:54:12+00:00

Can anyone tell me what causes this seemingly odd Do behavior in the following

  • 0

Can anyone tell me what causes this seemingly odd Do behavior in the following code? I would expect the Do handler to be called once per OnNext.

using System;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using NUnit.Framework;

[TestFixture]
public class WhyDoesDoActSoWierd
{
    [Test]
    public void ButWhy()
    {
        var doCount = 0;
        var observable = new Subject<int>();
        var stream = observable.Do( x => doCount++ );
        var subs =
            (from x in stream
             where x % 2 == 1
             from y in stream
             where y % 2 == 0
                   && y == x + 1
             select new { x, y })
                .Subscribe( x => Console.WriteLine( "{0}, {1}", x.x, x.y ) );

        observable.OnNext( 1 );
        // doCount == 1
        observable.OnNext( 2 );
        // doCount == 3
        observable.OnNext( 3 );
        // doCount == 5
        observable.OnNext( 4 );
        // doCount == 8
        observable.OnNext( 5 );
        // doCount == 11
        observable.OnNext( 6 );
        // doCount == 15
        Assert.AreEqual( 6, doCount );
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:54:13+00:00Added an answer on June 13, 2026 at 9:54 am

    The behaviour here is perfectly normal. The reason is that you don’t just have one subscription, you have many. And because of the cartesian product between the two observables in the query you have a larger number of Do than you might otherwise expect.

    Let’s look at an alternative (but similar) query to yours.

        var doCountX = 0;
        var doCountY = 0;
    
        Action dump = () =>
            Console.WriteLine("doCountX = {0}, doCountY = {1}", doCountX, doCountY);
    
        var observable = new Subject<int>();
    
        var streamX = observable.Do(x => doCountX++);
        var streamY = observable.Do(x => doCountY++);
    
        var query =
            from x in streamX
            from y in streamY
            select new { x, y };
    
        query.Subscribe(z => Console.WriteLine("{0}, {1}", z.x, z.y));
    
        dump();
        for (var i = 1; i <= 6; i++)
        {
            observable.OnNext(i);
            dump();
        }
    

    The output from this is:

    doCountX = 0, doCountY = 0
    doCountX = 1, doCountY = 0
    1, 2
    doCountX = 2, doCountY = 1
    1, 3
    2, 3
    doCountX = 3, doCountY = 3
    1, 4
    2, 4
    3, 4
    doCountX = 4, doCountY = 6
    1, 5
    2, 5
    3, 5
    4, 5
    doCountX = 5, doCountY = 10
    1, 6
    2, 6
    3, 6
    4, 6
    5, 6
    doCountX = 6, doCountY = 15
    

    There’s the initial dump of doCountX = 0, doCountY = 0 which is to be expected as this call to dump() occurs before any calls to OnNext.

    But when we get the first call to OnNext we don’t get a value produced by the query because the second streamY observable hasn’t yet been subscribed to.

    It’s only when OnNext is called the second time do we get a value from the query which happens to be the first OnNext value paired with the second. Now this also creates a new subscriptions to streamY, waiting for the next value.

    So we’ve now got the first two values from streamX waiting for the next value from the sequence. So when OnNext(3) is called we get two results.

    Each time this happens you can see the number of Do calls incrementing doCountY just keep going up.

    In fact, given this very plain SelectMany query the formula is:

    doCountY = n * (n - 1) / 2
    

    So with 6 values produced via OnNext you get doCountY equal to 6 * 5 / 2 or 15.

    Running with 10 values gives 10 * 9 / 2 or 45 values.

    So a SelectMany in fact does many more subscriptions than you might think. This is often why you would generally only use it to chain together observables that only produce a single value each to prevent the exponential explosion of subscriptions.

    Does it make sense now?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me why this code would not be working? $('body').on('test', function() {
Can anyone tell me why the following code fails to submit the form into
Can anyone tell me why this page causes a strange problem on Safari? If
Can anyone tell by looking at this style will cause a combobox with it
Can anyone tell me the general cause of this issue -[NSConcreteMutableData bytes]: message sent
I've been look at this for the last hour or so. Can anyone tell
Can anyone tell me why I am getting this exception? Well I know why
Can anyone tell me the cause of error ? Error is C:\web\template1.cpp||In function 'int
can anyone tell me how to select data by date in MS.ACCESS database using
Can anyone tell me how I can correctly pass my application context to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.