My first post here. I am learning C# coming from C/C++. Trying to "port" a project I am familiar with (Enigma cipher machine simulator) from C to C# as a learning exercise. The goal isn't to do a simple port, but rather to implement it in C# that it would be if written from scratch according to proper C# patterns.
Here is a structure in C that relies heavily on defines and everything is basically defines as uint8_t.
My first step was to replace the defines with constants.
#define POSITIONS 26
became:
public const int POSITIONS = 26;
The big issue I am running into is how to create static data structures in C#. I use this for keeping track of various machine derivatives and is is so easy to do in C:
So my question is how would you implement a structure like this in C#? I was really running into trouble with multiple issues with the arrays. I even saw a page that showed using a constructor and then multiple new calls to build it, but that seems so cumbersome. I've tried something like this:
Another issue is that trying to do the right thing and mark it const seemed impossible though it sounds like good practice. Searching showed that it couldn't be marked const because it was a reference type? It would seem odd that all reference types can't be marked const if that is true...
Thanks for all ideas and help!
Here is a structure in C that relies heavily on defines and everything is basically defines as uint8_t.
My first step was to replace the defines with constants.
#define POSITIONS 26
became:
public const int POSITIONS = 26;
The big issue I am running into is how to create static data structures in C#. I use this for keeping track of various machine derivatives and is is so easy to do in C:
C#:
struct MachineType
{
uint8_t Name[5],Mode,HasPlugboard,HasGapRotors,HasGearbox,EntryWheel,NoReflectors,ReflectorNames[4],Reflectors[4],NoRotors,RotorNames[8],Rotors[8];
} Machines[] =
{
//MACH_A865
{"A865", MODE_SU3R, 0, 0, 1, ROTOR_ETWQ, 1, "O", {ROTOR_REFLO},
3, "123", {ROTOR_1A, ROTOR_2A, ROTOR_3A}},
//MACH_D
{"D", MODE_SU3R, 0, 1, 0, ROTOR_ETWQ, 1, "O", {ROTOR_REFLO},
3, "123", {ROTOR_1O, ROTOR_2O, ROTOR_3O}},
};
So my question is how would you implement a structure like this in C#? I was really running into trouble with multiple issues with the arrays. I even saw a page that showed using a constructor and then multiple new calls to build it, but that seems so cumbersome. I've tried something like this:
C#:
//public struct testtype
//{
// public int var1, var2;
// public testtype(int Avar1, int Avar2)
// {
// var1 = Avar1;
// var2 = Avar2;
// }
//}
//public testtype[] test2 =
// {
// new testtype(4,5),
// new testtype(6,7)
// };
Another issue is that trying to do the right thing and mark it const seemed impossible though it sounds like good practice. Searching showed that it couldn't be marked const because it was a reference type? It would seem odd that all reference types can't be marked const if that is true...
Thanks for all ideas and help!