Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
What's the actual difference in stream and filestream. i mention two instances below please explain what's the difference in below two lines. both are doing same work but i want to understand actual difference?

// Stream s = new FileStream(filename, FileMode.Create);

// FileStream fs = File.Create(filename);
Posted

See the documentation:

Stream: https://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx[^]

FileStream: https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx[^]

FileStream is clearly more specific for files and Stream is the base class for FileStream.
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 9-Sep-15 16:23pm    
Updated your question to make it a candidate for my vote of 5. Hope that you don't mind. :-)

5ed.
This is a wrong, totally wrong question, as nearly all questions about "difference". In this particular case, this is the same as asking "what is the difference between penguin and bird?" Penguin is a bird, and FileStream is a Stream:
https://msdn.microsoft.com/en-us/library/system.io.filestream%28v=vs.110%29.aspx[^].

See also: https://msdn.microsoft.com/en-us/library/d62kzs03%28v=vs.110%29.aspx[^].

You really need to learn the basics of OOP.

In particular, you need to understand the compile-time types vs runtime types. In your first line, the constructor returns an instance of FileStream (as any constructor of the same time would do), but you abstract it up to the compile-time type Stream. Nevertheless, runtime type of the variable s is still FileStream. Please see: https://msdn.microsoft.com/en-us/library/47ek66wy%28v=vs.110%29.aspx[^].

And so on… Mind you, we are only discussing some prerequisites to OOP, but the "real" OOP plays fundamental role in these types; so you need to learn all those prerequisites (encapsulation, inheritance, and so on) really well, before you can really do any programming.

—SA
 
Share this answer
 
v4
Comments
Afzaal Ahmad Zeeshan 9-Sep-15 16:23pm    
Excellent answer, 5ed. I had the same answer in mind, but yours of penguin-bird is better!
Sergey Alexandrovich Kryukov 9-Sep-15 16:24pm    
Thank you, Afzaal. I knew that penguins rule. :-)
—SA
Afzaal Ahmad Zeeshan 9-Sep-15 16:32pm    
I was unaware, not anymore. Thank you, again! :-)
Sergey Alexandrovich Kryukov 9-Sep-15 16:40pm    
:-)

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