How to convert byte array to an object in C#
By Tan Lee Published on Oct 28, 2024 1.55K
To convert a byte array back to an object in C#, you can use deserialization.
How to convert byte array to an object in C#?
For example:
public static T FromByteArray<T>(byte[] data) { if (data != null) { using (MemoryStream ms = new MemoryStream(data)) { object obj = new BinaryFormatter().Deserialize(ms); return (T)obj; } } return default(T); }
Usage
[Serializable] public class People { public int Id { get; set; } public string Name { get; set; } } //Main.cs byte[] byteArray = /* your byte array here */; People obj = ByteArrayToObject<People>(byteArray);
When using BinaryFormatter
, make sure the class is marked with [Serializable]
.
Categories
Popular Posts
Structured Data using FoxLearn.JsonLd
Jun 20, 2025
Implement security headers for an ASP.NET Core
Jun 24, 2025
What Are RESTful Web Services?
Feb 19, 2024
Plus Admin Dashboard Template
Nov 18, 2024