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 813345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:20:37+00:00 2026-05-15T01:20:37+00:00

I have 3 TextReaders — a combination of StreamReaders and StringReaders. Conceptually, the concatenation

  • 0

I have 3 TextReaders — a combination of StreamReaders and StringReaders. Conceptually, the concatenation of them is a single text document.

I want to call a method (not under my control) that takes a single TextReader. Is there any built-in or easy way to make a concatenating TextReader from multiple TextReaders?

(I could write my own TextReader subclass, but it looks like a fair amount of work. In that case, I’d just write them all out to a temp file and then open it with a single StreamReader.)

Is there an easy solution to this that I’m missing?

  • 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-05-15T01:20:38+00:00Added an answer on May 15, 2026 at 1:20 am

    I just threw this together, so it’s not super-robust (no error handling, etc) but the basic test case works.

    It works by creating an extension method for TextReader‘s which take a second, and returns a new TextReader class which internally calls Read() on the first until it runs out, and then starts calling Read()on the second. You can chain this indefinitely.

    To provide a complete implementation of TextReader you only need to implement Read(), Peek(), Close() and Dispose(). All the other methods rely on specific implementation Read() to work. So creating your own TextReader really isn’t so bad, as you can see below.

    This also alleviates any performance concerns since we are simply wrapping the existing TextReaders and not actually invoking them to perform the concatenation.

    class Program
    {
        static void Main(string[] args)
        {
            StringReader first = new StringReader("hello ");
            StringReader second = new StringReader("world");
            StringReader third = new StringReader("!");
    
            using (var allOfThem = first.Concat(second).Concat(third))
            {
                //writes "hello world!"
                Console.WriteLine(allOfThem.ReadToEnd());
            }
            Console.Read();
        }
    }
    
    public static class Extensions
    {
        public static TextReader Concat(this TextReader first, TextReader second)
        {
            return new ChainedTextReader(first, second);
        }
    
        private class ChainedTextReader : TextReader
        {
            private TextReader first;
            private TextReader second;
            private bool readFirst = true;
    
            public ChainedTextReader(TextReader first, TextReader second)
            {
                this.first = first;
                this.second = second;
            }
    
            public override int Peek()
            {
                if (readFirst)
                {
                    return first.Peek();
                }
                else
                {
                    return second.Peek();
                }
            }
    
            public override int Read()
            {
                if (readFirst)
                {
                    int value = first.Read();
                    if (value == -1)
                    {
                        readFirst = false;
                    }
                    else
                    {
                        return value;
                    }
                }
                return second.Read();
            }
    
            public override void Close()
            {
                first.Close();
                second.Close();
            }
    
            protected override void Dispose(bool disposing)
            {
                base.Dispose(disposing);
                if (disposing)
                {
                    first.Dispose();
                    second.Dispose();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string in the format of [text][text][text][text][text] and I want to transform
I have a method set that uses pinvoke to call WM_GETTEXT on another program's
Have a look at this picture alt text http://www.abbeylegal.com/downloads/2009-04-01/web%20part%20top%20line.jpg Does anyone know what css
I have a text file in Program Files. I cannot write it from a
I have a web application which I uploaded using IIS. I want the users
I have an XmlReader that is trying to read text into a list of
I'm trying to make a dictionary game, and I have a text file with
I have written a boolean method which can be called upon to read information
I have created a method for reading a file in Java. The file in
I have a method that takes either a StringReader instance (reading from the clipboard)

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.