Convert String Array to MemoryStream C#.Net

How to convert string array to MemoryStream c#.net. This is simple code for converting a serialize string into MemoryStream for better performance.
In today's article I will show you a simple tutorial with an example for how you can convert or serialize the string array to memorystring using c#.net. This simple tutorial example you can use in asp.net core mvc application, console batch application and windows application.

So here we will use console application demonstrate. So, for this article we will create a new console application using c#.net. 

string [] stringArray = ["C#.net","VB.Net","Asp.Net",".Net MVC", "React Js","Angular Js","jQuery"];
var jsonValue = JsonSerializer.Serialize(stringArray);
byte[] byteArray = Encoding.UTF8.GetBytes(jsonValue);
var memoryStreamData = new MemoryStream(byteArray); 
Now let's discuss each and every line written in above code. Let's check the first line of code. 
string [] stringArray = ["C#.net","VB.Net","Asp.Net",".Net MVC", "React Js","Angular Js","jQuery"];
In above code i have declared the array of string type. Here in this array i have added some values. There we going to convert into memory stream by using MemoryStream(byteArray). So before using MemoryStream(byteArray) we need to perform few steps. 
var jsonValue = JsonSerializer.Serialize(stringArray);
Here in above code, I have converted the array of string into Json string by using JsonSerializer.Serialize(stringArray). This will convert array into string.

String Array to Json String in C#.Net

Now question is coming why we are converting the string array into Json string. This is for getting the byte array by using Encoding.UTF8.GetBytes(string). As we cannot directly convert the array to memory stream, first we need to convert it into string ant then convert it to byte array. This byte array we will pass to MemoryStream to convert it into stream in c#.net.
byte[] byteArray = Encoding.UTF8.GetBytes(jsonValue);
Now let's check what we are getting in the byte array. 

ByteArray in C#.Net

Bye array we will pass it in the MemoryStream(byteArray) to convert into the MemoryStream. Please check the below piece of code.
var memoryStreamData = new MemoryStream(byteArray); 
Let's run and check the output.

MemoryStream C#

Post a Comment