Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
3.89/5 (2 votes)
See more:
Hello everyone,

I am new to object programming, and I have a question in that topic:

I understand that when we instantiate an object we do it like so:

C#
ClassName objectName= new ClassName();


What does the following line of code mean:

C#
TextReader reader = new StreamReader(filename);


It looks like an object instantiation, am I wrong ?

Thanks for your help
Posted
Updated 8-Dec-14 16:13pm
v5

C#
TextReader reader = new StreamReader(filename);

Instantiates an object of the StreamReader Class into a TextReader variable named reader.

It would probably be better to use a StreamReader variable.
C#
StreamReader reader = new StreamReader(filename);


The StreamReader Class is inherited from the TextReader Class. The StreamReader Class has additional features that won't be available if the TextReader Class is used for the variable data type.

See StreamReader Class[^]
 
Share this answer
 
v4
You are correct. It instantiates a StreamReader.
 
Share this answer
 
Most cases, we use class name to define type of a variable and one of its constructor with new keyword while instantiation of an object. A class can have many constructors....you can pick any based on your need. I have edited the question also as you wrote "constructor1" at left hand side of first code line. It should be class name. I understand the reason of confusion: "The constructor is a method in a class with same name". And in C#...."If you do not provide a constructor for your object, C# will create one by default that instantiates the object".
For more, please have a look on:

http://msdn.microsoft.com/en-us/library/ms173115.aspx[^]
and
An Intro to Constructors in C#[^]

And as already other fellows confirmed...you are right...that StreamReader is getting instantiated...in your case it is using a particular constructor which takes file name. And on RHS, you have TextReader because TextReader is a parent class of StreamReader so you are creating the instance of SreamReader and putting it into a variable which is a type of "TextReader".
public class StreamReader : TextReader


Your code "TextReader reader = new StreamReader(filename);"..... is equivalent to below code:
TextReader reader;
StreamReader s1;
s1 = new StreamReader(filename);
reader = s1;

As given at http://msdn.microsoft.com/en-us/library/system.io.streamreader(v=vs.110).aspx[^], StremReader class is having many constructors and inherited from "TextReader".
Feel free to discuss more. Thanks.
 
Share this answer
 
v9
Comments
saadmechiche 9-Dec-14 6:14am    
Thanks for your detailed answer
Snesh Prajapati 9-Dec-14 6:25am    
You are most welcome :-)
StreamReader class Implements a TextReader that reads characters from a byte stream in a particular encoding.

here you create the instance of StreamReader Class into a TextReader variable named reader.
 
Share this answer
 
Comments
Agent__007 9-Dec-14 5:29am    
StreamReader class doesn't "implement" TextReader, it "inherits" or "extends" it. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900