This happens twice in the Sherlock Holmes canon, openly inspired by Poe's Dupin.
The opening passage of "The Adventure of the Dancing Men", from the collection The Return of Sherlock Holmes (1905), is as follows:
Holmes had been seated for some hours in silence with his long, thin back curved over a chemical vessel in which he was brewing a particularly malodorous product. His head was sunk upon his breast, and he looked from my point of view like a strange, lank bird, with dull grey plumage and a black top-knot.
“So, Watson,” said he, suddenly, “you do not propose to invest in South African securities?”
I gave a start of astonishment. Accustomed as I was to Holmes's curious faculties, this sudden intrusion into my most intimate thoughts was utterly inexplicable.
“How on earth do you know that?” I asked.
He wheeled round upon his stool, with a steaming test-tube in his hand and a gleam of amusement in his deep-set eyes.
“Now, Watson, confess yourself utterly taken aback,” said he.
“I ought to make you sign a paper to that effect.”
“Because in five minutes you will say that it is all so absurdly simple.”
“I am sure that I shall say nothing of the kind.”
“You see, my dear Watson”—he propped his test-tube in the rack and began to lecture with the air of a professor addressing his class—“it is not really difficult to construct a series of inferences, each dependent upon its predecessor and each simple in itself. If, after doing so, one simply knocks out all the central inferences and presents one's audience with the starting-point and the conclusion, one may produce a startling, though possibly a meretricious, effect. Now, it was not really difficult, by an inspection of the groove between your left forefinger and thumb, to feel sure that you did NOT propose to invest your small capital in the goldfields.”
“I see no connection.”
“Very likely not; but I can quickly show you a close connection. Here are the missing links of the very simple chain: 1. You had chalk between your left finger and thumb when you returned from the club last night. 2. You put chalk there when you play billiards to steady the cue. 3. You never play billiards except with Thurston. 4. You told me four weeks ago that Thurston had an option on some South African property which would expire in a month, and which he desired you to share with him. 5. Your cheque-book is locked in my drawer, and you have not asked for the key. 6. You do not propose to invest your money in this manner.”
“How absurdly simple!” I cried.
Early in the controversial story "The Adventure of the Cardboard Box", from the collection Memoirs of Sherlock Holmes (1894), a similar conversation takes place:
Finding that Holmes was too absorbed for conversation I had tossed side the barren paper, and leaning back in my chair I fell into a brown study.Suddenly my companion’s voice broke in upon mythoughts: “You are right, Watson,” said he. “It does seem a most preposterous way of settling a dispute.”
“Most preposterous!” I exclaimed, and then suddenly realizing how he had echoed the inmost thought of my soul, I sat up in my chair and stared at him in blank amazement.
“What is this, Holmes?” I cried. “This is beyond anything which I could have imagined.”
He laughed heartily at my perplexity.
“You remember,” said he, “that some little time ago when I read you the passage in one of Poe’s sketches in which a close reasoner follows the unspoken thoughts of his companion, you were inclined to treat the matter as a mere tour-de-force of the author. On my remarking that I was constantly in the habit of doing the same thing you expressed incredulity.”
“Then I will tell you. After throwing down your paper, which was the action which drew my attention to you, you sat for half a minute with a vacant expression. Then your eyes fixed themselves upon your newly framed picture of General Gordon, and I saw by the alteration in your face that a train of thought had been started. But it did not lead very far. Your eyes flashed across to the unframed portrait of Henry Ward Beecher which stands upon the top of your books. Then you glanced up at the wall, and of course your meaning was obvious. You were thinking that if the portrait were framed it would just cover that bare space and correspond with Gordon’s picture there.”
“You have followed me wonderfully!” I exclaimed.
“So far I could hardly have gone astray. But now your thoughts went back to Beecher, and you looked hard across as if you were studying the character in his features. Then your eyes ceased to pucker, but you continued to look across, and your face was thoughtful. You were recalling the incidents of Beecher’s career. I was well aware that you could not do this without thinking of the mission which he undertook on behalf of the North at the time of the Civil War, for I remember your expressing your passionate indignation at the way in which he was received by the more turbulent of our people. You felt so strongly about it that I knew you could not think of Beecher without thinking of that also. When a moment later I saw your eyes wander away from the picture, I suspected that your mind had now turned to the Civil War, and when I observed that your lips set, your eyes sparkled, and your hands clenched I was positive that you were indeed thinking of the gallantry which was shown by both sides in that desperate struggle. But then, again, your face grew sadder, you shook your head. You were dwelling upon the sadness and horror and useless waste of life. Your hand stole towards your own old wound and a smile quivered on your lips, which showed me that the ridiculous side of this method of settling international questions had forced itself upon your mind. At this point I agreed with you that it was preposterous and was glad to find that all my deductions had been correct.”
It's worth noting that, way back in the very first Sherlock Holmes story A Study in Scarlet, Holmes makes reference to this trick as used by Poe, but with a very different tone:
Sherlock Holmes rose and lit his pipe. "No doubt you think that you are complimenting me in comparing me to Dupin," he observed. "Now, in my opinion, Dupin was a very inferior fellow. That trick of his of breaking in on his friends' thoughts with an apropos remark after a quarter of an hour's silence is really very showy and superficial. He had some analytical genius, no doubt; but he was by no means such a phenomenon as Poe appeared to imagine."
For completeness, I include here the passage from Edgar Allan Poe's first Dupin story The Murders in the Rue Morgue which inspired Arthur Conan Doyle to write similar scenes for his own detective character:
We were strolling one night down a long dirty street in the vicinity of the Palais Royal. Being both, apparently, occupied with thought, neither of us had spoken a syllable for fifteen minutes at least. All at once Dupin broke forth with these words:
“He is a very little fellow, that’s true, and would do better for the Théâtre des Variétés.”
“There can be no doubt of that,” I replied unwittingly, and not at first observing (so much had I been absorbed in reflection) the extraordinary manner in which the speaker had chimed in with my meditations. In an instant afterward I recollected myself, and my astonishment was profound.
“Dupin,” said I, gravely, “this is beyond my comprehension. I do not hesitate to say that I am amazed, and can scarcely credit my senses. How was it possible you should know I was thinking of ——-?” Here I paused, to ascertain beyond a doubt whether he really knew of whom I thought.
—“of Chantilly,” said he, “why do you pause? You were remarking to yourself that his diminutive figure unfitted him for tragedy.”
This was precisely what had formed the subject of my reflections. Chantilly was a quondam cobbler of the Rue St. Denis, who, becoming stage-mad, had attempted the rôle of Xerxes, in Crébillon’s tragedy so called, and been notoriously Pasquinaded for his pains.
“Tell me, for Heaven’s sake,” I exclaimed, “the method—if method there is—by which you have been enabled to fathom my soul in this matter.” In fact I was even more startled than I would have been willing to express.
“It was the fruiterer,” replied my friend, “who brought you to the conclusion that the mender of soles was not of sufficient height for Xerxes et id genus omne.”
“The fruiterer!—you astonish me—I know no fruiterer whomsoever.”
“The man who ran up against you as we entered the street—it may have been fifteen minutes ago.”
I now remembered that, in fact, a fruiterer, carrying upon his head a large basket of apples, had nearly thrown me down, by accident, as we passed from the Rue C —— into the thoroughfare where we stood; but what this had to do with Chantilly I could not possibly understand.
There was not a particle of charlatanerie about Dupin. “I will explain,” he said, “and that you may comprehend all clearly, we will first retrace the course of your meditations, from the moment in which I spoke to you until that of the rencontre with the fruiterer in question. The larger links of the chain run thus—Chantilly, Orion, Dr. Nichols, Epicurus, Stereotomy, the street stones, the fruiterer.”
There are few persons who have not, at some period of their lives, amused themselves in retracing the steps by which particular conclusions of their own minds have been attained. The occupation is often full of interest and he who attempts it for the first time is astonished by the apparently illimitable distance and incoherence between the starting-point and the goal. What, then, must have been my amazement when I heard the Frenchman speak what he had just spoken, and when I could not help acknowledging that he had spoken the truth. He continued:
“We had been talking of horses, if I remember aright, just before leaving the Rue C ——. This was the last subject we discussed. As we crossed into this street, a fruiterer, with a large basket upon his head, brushing quickly past us, thrust you upon a pile of paving stones collected at a spot where the causeway is undergoing repair. You stepped upon one of the loose fragments, slipped, slightly strained your ankle, appeared vexed or sulky, muttered a few words, turned to look at the pile, and then proceeded in silence. I was not particularly attentive to what you did; but observation has become with me, of late, a species of necessity.
“You kept your eyes upon the ground—glancing, with a petulant expression, at the holes and ruts in the pavement, (so that I saw you were still thinking of the stones,) until we reached the little alley called Lamartine, which has been paved, by way of experiment, with the overlapping and riveted blocks. Here your countenance brightened up, and, perceiving your lips move, I could not doubt that you murmured the word ‘stereotomy,’ a term very affectedly applied to this species of pavement. I knew that you could not say to yourself ‘stereotomy’ without being brought to think of atomies, and thus of the theories of Epicurus; and since, when we discussed this subject not very long ago, I mentioned to you how singularly, yet with how little notice, the vague guesses of that noble Greek had met with confirmation in the late nebular cosmogony, I felt that you could not avoid casting your eyes upward to the great nebula in Orion, and I certainly expected that you would do so. You did look up; and I was now assured that I had correctly followed your steps. But in that bitter tirade upon Chantilly, which appeared in yesterday’s ‘Musée,’ the satirist, making some disgraceful allusions to the cobbler’s change of name upon assuming the buskin, quoted a Latin line about which we have often conversed. I mean the line
Perdidit antiquum litera sonum.
“I had told you that this was in reference to Orion, formerly written Urion; and, from certain pungencies connected with this explanation, I was aware that you could not have forgotten it. It was clear, therefore, that you would not fail to combine the two ideas of Orion and Chantilly. That you did combine them I saw by the character of the smile which passed over your lips. You thought of the poor cobbler’s immolation. So far, you had been stooping in your gait; but now I saw you draw yourself up to your full height. I was then sure that you reflected upon the diminutive figure of Chantilly. At this point I interrupted your meditations to remark that as, in fact, he was a very little fellow—that Chantilly—he would do better at the Théâtre des Variétés.”
There is no doubt that this is the passage which inspired Doyle and Holmes, because in "The Adventure of the Cardboard Box" Watson mentioned that in the passage which Holmes read to him, "If I remember right, [Poe's character] stumbled over a heap of stones, looked up at the stars, and so on."