C# program to demonstrate MemoryStream class

We hope you find the C# program to demonstrate MemoryStream class article in mind. Let’s move on to the articles.

C# program to demonstrate MemoryStream class

C# program to demonstrate MemoryStream class

//C# program to demonstrate memory stream class.

using System;
using System.IO;
using System.Text;

class Demo
{
    static void Main()
    {
        
        MemoryStream memoryStream;
        int count=0;

        byte[] byteArray;
        byte[] byteBuff1 = { 65, 67, 68 };
        byte[] byteBuff2 = { 69, 70, 71 }; 
        
        memoryStream = new MemoryStream(50);

        memoryStream.Write(byteBuff1, 0, 3);        
        while (count < byteBuff2.Length)
        {
            memoryStream.WriteByte(byteBuff2[count++]);
        }

        memoryStream.Seek(0, SeekOrigin.Begin);

        byteArray = new byte[memoryStream.Length];
        count = memoryStream.Read(byteArray, 0, byteArray.Length);

        memoryStream.Close();

        Console.WriteLine("Data written into memory stream:");
        foreach (byte b in byteArray)
        {
            Console.Write((char)b);
        }
        Console.WriteLine();
    }
}
Data written into memory stream:
ACDEFG
Press any key to continue . . .

Final Words

We hope you find the C# program to demonstrate MemoryStream class article very useful. If you have any doubts you can ask your doubts through the comment box in this article

Share on:

Leave a Comment