C# Program to Demonstrate Memorystream Class

You are currently viewing 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 . . .

Ranjith Kumar

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply