-1

In .NET Framework Class Library, a book says that

Console.In is an instance of TextReader.

TextReader is an abstract class, so how can it have an instance Console.In?

StreamReader is a derived class of TextReader, and is a non-abstract class. Is Console.In an instance of StreamReader?

soundslikeodd
  • 1,078
  • 3
  • 19
  • 32
Tim
  • 1
  • 141
  • 372
  • 590

4 Answers4

2

TextReader is an abstract class, so how can it have an instance Console.In?

You're logically correct, but its (I would say) generally understood that when you say "I have an instance of [an abstract class]" what you really mean is "I have an instance of a concrete class which inherits an abstract class". It's the same with interfaces, you might say "I have an instance of IFoo" where you actually mean "I have an instance of a class which conforms to the contract set out by IFoo".

For your specific question, the disassembly is available And you can see that you're basically right. The actual type is of course a concrete class which inherits TextReader. It uses TextReader.Synchronized around a StreamReader instance.

....

tr = TextReader.Synchronized(new StreamReader(s, enc, false, 
                                              _DefaultConsoleBufferSize, false));
....
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Upvote for the clearer explanation of the wording. (and just to be complete: under that `StreamReader` there is yet another `__ConsoleStream`, the `s` in the constructor call you show). – René Vogt Feb 16 '17 at 17:15
1

Try

Console.WriteLine(Console.In.GetType());

The result is

System.IO.TextReader+SyncTextReader

So Console.In is of type System.IO.TextReader.SyncTextReader which is a class nested in TextReader.

Only the property Console.In is declared as TextReader which SyncTextReader obviously derives from.

So in terms of inheritance, the sentence "Console.In is an instance of TextReader" is correct.


Update: Indeed (as Jamiec commented) this SyncTextReader has an underlying instance of StreamReader. This in turn is created with an underlying instance of type __ConsoleStream.

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • You sure its not just the call to `TextReader.Synchronized` which returns that type. Underneath it does indeed look like a `StreamReader` from the [disassembly](http://www.dotnetframework.org/default.aspx/4@0/4@0/DEVDIV_TFS/Dev10/Releases/RTMRel/ndp/clr/src/BCL/System/Console@cs/1305376/Console@cs) – Jamiec Feb 16 '17 at 17:06
  • @Jamiec update my answer with the decoration chain as I could track it down – René Vogt Feb 16 '17 at 17:12
0

The base class of Console.In is SynctextReader which derives from TextReader.

Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
0
Console.WriteLine(Console.In.GetType().FullName);

gives us

System.IO.TextReader+SyncTextReader

SyncTextReader is nested class inside TextReader. It is non abstract, but internal See https://referencesource.microsoft.com/#mscorlib/system/io/textreader.cs,311

tym32167
  • 4,741
  • 2
  • 28
  • 32