in this case a binary file is written with the file format based on a struct
struct fileformat
{
struct mask
{
bool mem1present
bool mem2present
bool mem3present
//5 bits unused
}
//member only written in file if mem1present is true
byte mem1present
//member only written in file if mem2present is true
byte mem1present
//member only written in file if mem3present is true
byte mem1present
}
is this possible to be implemented in c#
Sure – you have to implement the serialization yourself to some extent, but you can do that easily enough.
It’s unclear what sort of serialization you’re using – if you’re using the “raw” binary serialization from .NET, you want to override
GetObjectDatato only add the relevant data on serialization, and then in the protected constructor taking aSerializationInfoand aStreamingContext, populate your struct from the same data in reverse. See this MSDN article for some details.I don’t know what happens if you’re using XML serialization.
If you’re writing your own serialization (i.e. you’ve got a method such as
WriteToStream) then you can choose to represent it however you want, of course.EDIT: It sounds like you’ve probably got an existing file format you need to read in, but you can define your own types. It’s easy to have a class or struct with multiple members and possibly a mask to say what’s set, although without knowing more it may not be the best design. While you can use explicit layout to make this efficient in memory, it’s probably easiest just to have separate members:
Then somewhere (either in the data type or not), something like:
By the way, I would seriously consider whether a struct is really the best approach here – consider using a class instead. I very rarely create my own structs.