C# Program to Demonstrate Memory Stream Class


This is a C# Program to illustrate memory stream class.

Problem Description

This C# Program Illustrates Memory Stream Class.

Problem Solution

Here the below program shows how to read and write data using memory as a backing store.

Program/Source Code

Here is source code of the C# Program to Illustrate Memory Stream Class. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

 * C# Program to Illustrate Memory Stream Class
using System;
using System.IO;
using System.Text;
class MemStream
    static void Main()
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        byte[] firstString=uniEncoding.GetBytes("Invalid file path characters are: ");
        byte[] secondString = uniEncoding.GetBytes(Path.GetInvalidPathChars());
        using(MemoryStream memStream = new MemoryStream(100))
            memStream.Write(firstString, 0 , firstString.Length);
            count = 0;
            while(count < secondString.Length)
            Console.WriteLine("Capacity = {0}, Length = {1}, Position = {2}\n",
            memStream.Seek(0, SeekOrigin.Begin);
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);
            while (count < memStream.Length)
                byteArray[count++] = Convert.ToByte(memStream.ReadByte());
            charArray = new char[uniEncoding.GetCharCount(byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(byteArray, 0, count, charArray, 0);
Program Explanation

This C# program is used to illustrate memory stream class. The firstString variable is used to display the invalid file path characters and secondString variable is used to get an array containing the characters that are not allowed in path names using GetInvalidPathChars().

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

While loop is used to check the value of ‘count’ variable is less than the value of the length of ‘secondString’ variable. If the condition is true then execute the iteration of the loop. The WriteByte() function is used to write a byte to the current position in the file stream.

The memStream.Capacity.ToString() function is used to get or set the maximum number of characters contained in the memory allocated by the current instance. The memStream.Length.ToString() function is used to get the number of characters in the current string object.

The seek() function is used to specify the position in a stream to use for seeking. Program shows how to read and write data using memory as a backing store.

Runtime Test Cases
Capacity = 256  Length = 140  Position =140
Invalid File Path Characters are : "<>|

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.


Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.