0

System.io.stream is a abstract class then how httpwebrequest.getrequeststream() return the instance of stream class. i.e

 Stream serverStream  = request.GetRequestStream();

How stream class is initialized?

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
user1075564
  • 21
  • 2
  • 8

3 Answers3

0

httpwebrequest.getrequeststream() returns one of the concrete classes that inherit from the abstract class System.IO.Stream.

potatopeelings
  • 40,709
  • 7
  • 95
  • 119
  • "The GetRequestStream method initiates a request to send data to the Internet resource and returns a Stream instance for sending data to the Internet resource." this is what msdn says and its seems not returning the concrete inherited class like file stream or memory stream – user1075564 May 30 '15 at 07:26
  • Do a Debug.WriteLine of serverStream.GetType().FullName and you'll get the actual concrete class. MSDN indicates that it could be any of the concrete classes inherited from Stream (an instance of x mostly means x and any class which can fill in for x) – potatopeelings May 30 '15 at 07:36
0

Stream is the abstract base class of all streams.

The Stream class and its derived classes provide a generic view of these different types of input and output, and isolate the programmer from the specific details of the operating system and the underlying devices.

and

httpwebrequest.getrequeststream 

Gets a Stream object to use to write request data.

You have to create an instance of stream subclasses to get initialized.

go through this and this SO post for more .

Community
  • 1
  • 1
Tharif
  • 13,794
  • 9
  • 55
  • 77
  • The code in question is working code and i am not looking for initializing the stream class using subclass. my question is how the above code is initializing the abstract class – user1075564 May 30 '15 at 07:30
  • nope i had not created a instance i can't create a instance of stream class as its an abstract class using the new operator. so just wondering how this statement "Stream serverStream = request.GetRequestStream();" is initializing the stream class – user1075564 May 30 '15 at 07:37
0

You are confusing the type of a reference with the type of an object.

The fact that a method signature declares a particular type as its return type does not mean that instances returned will be of that exact type, they could be of a type derived from the one specified, as in you case.

Try this: Console.WriteLine(stream.GetType()), should clear your thoughts a bit.

MatteoSp
  • 2,940
  • 5
  • 28
  • 36