Reputation: 1651
I found that this problem is not addressed in this knowledge base and decided to add the question and the answer to help others.
I am creating a TextReader
to read through a text file line by line using ReadLine
, which requires a termination check for the end of this stream.
TextReader TR = new TextReader("MyFile.txt");
while (!TR.EndOfStream) // fails to compile here
{
// do something
}
The EndOfStream does not exist for the TextReader class and this code will not compile.
Upvotes: 1
Views: 318
Reputation: 1651
The solution is relatively simple but may be missed by those new to C# and the .NET libraries (as well as experts who write code too quickly!).
The TextReader
class is subclassed from StreamReader
, and it is this latter class which provides the common functionality for all of its child classes.
Thus the solution is to declare the reader as a StreamReader
, and then to instantiate it as a TextReader
.
StreamReader TR = new TextReader("MyFile.txt");
while (!TR.EndOfStream) // it finds this property now
{
// do something
}
Upvotes: -1