What is the correct way to convert my class to/from a byte array? I want to be able to do thing like save it to a file as simply as possible.
Should I make it serializable?
Should I just supply a function that returns a byte array? Or maybe a MemoryStream?
Something else?
I am not overly familiar with C# file handling, so if the answer could also show what the common way of writing binary data to a file is, I would appreciate it.
Edit I see from the answers that I am misunderstood. I have a class. I have a format I want to save it in binary form in. I want to know what the correct way is to expose this binary representation. I need full control of the format, not .NET doing it for me (I will be reading it later in c++)
In response to your edit, and given the fact that you own the binary format and need to control it yourself, I see two options:
Of course, you could decouple this from the type itself by creating another class with
The first option of each pair probably gives you more flexibility; you could pass a file stream or a memory stream or whatnot depending on your needs.