Question: I'm converting some code from C++ to C# as a method to learn C#. In my C++ code I have

```
struct ChArray{
    char ch[8];
};

bool operator < (const ChArray& arrA, const ChArray& arrB);

```

In my C++, I can do things like:

```
map<ChArray, double>::iterator s;
for(s = settings.begin(); s != settings.end(); s++){
    out.write(reinterpret_cast<const char*>(&s->first), sizeof(ChArray));
}

```

The question is what is necessary to accomplish the same things in C#?

Not the map part (I know to use Dictionary), but rather, how do I create a set sized structure that can be easily written to and read from a file?

I've tried several combinations, all result in some error which seems like it would cause a problem later on when writing.

For instance, with a structure, I can't define the default size, which means I have to use new to dynamically allocate memory, which means (to me as a C++ programmer) that there would be no accurate way to guarantee the size of the structure when the file is being read, because the dynamic allocation could have been anything when the structure was initialized.

Maybe I'm just not aware of some c# power that would clear up this mess.

Answer: 
C# e.g. provides `BinaryReader` and `BinaryWriter` that allowes you to read and write Byte arrays from and to a file.

I believe your problem is, that you try to convert c++ to c# code on too low a level, where systemIO is concerned

EDIT: Maybe I missunderstand you, but If you want to directly translate your c++ code into c# code what's wrong with using the following struct: 

```
class Ch8Array
{
    public byte[] ch = new byte[8];
}

```

and read/write via:

```
Ch8Array arr = new Ch8Array();

using (BinaryReader reader = new BinaryReader(File.Open("test", FileMode.Open))) {
    reader.Read(arr.ch, 0, arr.ch.Length);
} 

using (BinaryWriter writer = new BinaryWriter(File.Open("test2", FileMode.Create))) {
    writer.Write(arr.ch);
}   

```

(for sake of clarity I omitted the map part)
I recommend that you try to work at a higher level in C#. Especially if you want to get to know the language. C# trades of some low-level capabilities for vastly increased productivity. Embrace that.

What you want is directly translatable to C# using [Fixed Size Buffers](http://msdn.microsoft.com/en-us/library/zycewsya.aspx). Alternatively, you could use the `StructLayoutAttribute` to set a fixed size for a struct and control member layout. C# supports unsafe code quite well. This is often used for interop with native APIs.