Table of Contents

Class TextReaderExtensions

Namespace
Cuemon.Extensions.IO
Assembly
Cuemon.Extensions.IO.dll

Extension methods for the TextReader class.

public static class TextReaderExtensions
Inheritance
TextReaderExtensions

Examples

TextReaderExtensions provides extension methods for TextReader including ReadAllLines, ReadAllLinesAsync, and CopyToAsync for reading and copying text content. This example creates a multi-line string "line one\nline two\nline three", converts it to a TextReader via ToTextReader, then calls ReadAllLines synchronously and ReadAllLinesAsync asynchronously to collect each line. It also uses CopyToAsync to write the reader content into a StringWriter. Console output confirms all three approaches produce the correct line count of 3 and that the copied content contains "line two".

using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Cuemon.Extensions.IO;

namespace MyApp.Examples
{
    public static class TextReaderExtensionsExample
    {
        public static async Task DemonstrateAsync()
        {
            const string input = "line one\nline two\nline three";

            using var linesReader = input.ToTextReader();
            var lines = linesReader.ReadAllLines().ToList();

            using var asyncLinesReader = input.ToTextReader();
            using var writer = new StringWriter();
            var asyncLines = await asyncLinesReader.ReadAllLinesAsync();

            using var copyReader = input.ToTextReader();
            await copyReader.CopyToAsync(writer);

            Console.WriteLine(lines.Count);
            Console.WriteLine(asyncLines.Count);
            Console.WriteLine(writer.ToString().Contains("line two"));
        }
    }
}

Methods

CopyToAsync(TextReader, TextWriter, int)

Asynchronously reads the bytes from the reader and writes them to the writer.

public static Task CopyToAsync(this TextReader reader, TextWriter writer, int bufferSize = 81920)

Parameters

reader TextReader

The TextReader to extend.

writer TextWriter

The TextWriter to asynchronously write bytes to.

bufferSize int

The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.

Returns

Task

A task that represents the asynchronous copy operation.

Exceptions

ArgumentNullException

reader cannot be null -or- writer cannot be null.

ArgumentOutOfRangeException

bufferSize is lower than or equal to 0.

ReadAllLines(TextReader)

Reads all lines of characters from the reader and returns the data as a sequence of strings.

public static IEnumerable<string> ReadAllLines(this TextReader reader)

Parameters

reader TextReader

The TextReader to extend.

Returns

IEnumerable<string>

An IEnumerable{string} that contains all lines of characters from the reader.

ReadAllLinesAsync(TextReader)

Asynchronously reads all lines of characters from the reader and returns the data as a sequence of strings.

public static Task<IReadOnlyList<string>> ReadAllLinesAsync(this TextReader reader)

Parameters

reader TextReader

The TextReader to extend.

Returns

Task<IReadOnlyList<string>>

A task that represents the asynchronous operation. The task result contains a IReadOnlyList{string} that contains all lines of characters from the reader that contains elements from the input sequence.