Home

readtoend

ReadToEnd is a method in the .NET framework, defined on the StreamReader class and inherited from TextReader. It reads all remaining characters from the current position in the stream to its end and returns them as a string.

The method advances the reader to the end of the stream and returns the collected content. If

Usage is common when the full contents of a file or stream are needed in memory. A

using (var reader = new StreamReader("path/to/file.txt"))

{

string content = reader.ReadToEnd();

}

There is also an asynchronous variant, ReadToEndAsync, which returns a Task<string> and is suitable for non-blocking

Considerations and alternatives: ReadToEnd loads the entire remaining content into memory, which can be problematic for

See also: TextReader.ReadToEnd, StreamReader, ReadLine, ReadAsync, ReadToEndAsync.

the
end
of
the
stream
has
already
been
reached,
it
returns
an
empty
string.
It
can
throw
ObjectDisposedException
if
the
reader
has
been
disposed,
or
IOException
for
I/O
errors;
creating
the
resulting
string
may
also
throw
OutOfMemoryException
if
the
content
is
very
large.
typical
pattern
is:
contexts.
very
large
files
or
constrained
environments.
For
large
data
or
streaming
scenarios,
reading
in
chunks
with
Read
or
ReadAsync,
or
processing
line
by
line
with
ReadLine,
may
be
preferable.
ReadToEndAsync
provides
the
asynchronous
alternative
to
ReadToEnd.