TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# BaseStream Property

Use the BaseStream instance property. A BaseStream can be accessed on a BinaryReader.
BaseStream is a property on a Stream. It returns information about the underlying buffer. BaseStream is available on all derived Stream instances. This property is useful in many programs.StreamFile
This program uses 2 System.IO objects. The MemoryStream stores the bytes in the specific file. And the BinaryReader type exposes the BaseStream property. The 2 using-statements enclose the BinaryReader object.Using

Tip: You can see that the BaseStream returns a reference to the MemoryStream instance that was passed to the BinaryReader.

Info: The Stream returned by BaseStream is precisely equivalent to the MemoryStream. It is, however, referenced through its base class Stream.

MemoryStream
C# program that uses BaseStream property using System; using System.IO; class Program { static void Main() { using (MemoryStream memory = new MemoryStream(File.ReadAllBytes("C:\\P.bin"))) using (BinaryReader reader = new BinaryReader(memory)) { // Get the BaseStream Stream. Stream baseStream = reader.BaseStream; // The BaseStream is the MemoryStream instance. Console.WriteLine(baseStream.Length == memory.Length); Console.WriteLine(baseStream is MemoryStream); } } } Output True True
Discussion. The BaseStream property is defined on several types, including BinaryWriter, BinaryReader, StreamWriter and StreamReader. The best reason to use BaseStream is if your program's design doesn't allow you to directly access the base stream.BinaryWriterBinaryReaderStreamWriterStreamReader
If you design a method that receives a BinaryReader parameter directly, you can call BaseStream to get the underlying Stream reference. In the example shown above, we could easily access the MemoryStream. This is not always possible.
Summary. BaseStream is useful when using classes that act upon underlying Streams. Often we need a reference to the original Stream to get its total Length or type. This helps when developing methods that receive types with underlying Streams.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf