How to create static data structures ?

alank2

Member
Joined
Jan 10, 2019
Messages
6
Programming Experience
10+
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:

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!
 
I don't have time to address this question fully right now but, on the subject of constants, they can only be set using literal values or other constants. As reference types generally don't have literal representations, they can't be constants either. The obvious exception is the String class. You can have literal Strings so you can also have String constants. The alternative is a field declared 'readonly'.
 
You may try this code to make static data structures.

public static class MyStaticClass
{
public static int myStaticVariable = 0;

public static void MyStaticMethod()
{
Console.WriteLine("This is a static method.");
}

public static int MyStaticProperty { get; set; }
}

class Program
{
static void Main(string[] args)
{

Console.WriteLine(MyStaticClass.myStaticVariable);

MyStaticClass.MyStaticMethod();

MyStaticClass.MyStaticProperty = 100;

Console.WriteLine(MyStaticClass.MyStaticProperty);
}
}
 
For the MyStaticProperty - what is the point of { get; set; }

Does that just make it a property?
Without the { get; set; } would it just be a field?
What is the difference? I don't see why giving it a get/set is worth anything if they just offer full rights to read and write the variable anyway...
 
You may try this code to make static data structures.

public static class MyStaticClass
{
public static int myStaticVariable = 0;

public static void MyStaticMethod()
{
Console.WriteLine("This is a static method.");
}

public static int MyStaticProperty { get; set; }
}

class Program
{
static void Main(string[] args)
{

Console.WriteLine(MyStaticClass.myStaticVariable);

MyStaticClass.MyStaticMethod();

MyStaticClass.MyStaticProperty = 100;

Console.WriteLine(MyStaticClass.MyStaticProperty);
}
}

When the OP says "static" they don't mean in the sense of the 'static' keyword in C#, i.e. members of a class as opposed to members of an instance of a class, but rather having values that don't change.
 
For the MyStaticProperty - what is the point of { get; set; }

Does that just make it a property?
Without the { get; set; } would it just be a field?
What is the difference? I don't see why giving it a get/set is worth anything if they just offer full rights to read and write the variable anyway...

Yes, the getter and setter make the member a property rather than a field. Public members should pretty much always be properties rather than fields but the major exception is when the data cannot change. You'll find that a number of types throughout the .NET Framework have public constants or, more generally, read-only fields. As I said previously, if you want constant-like behaviour for a type that cannot be declared constant, use a read-only field. There are some differences between the two and I think that one is that constants are effectively static in the C# sense, i.e. they are members of the type rather than members of an instance of the type, whereas fields are instance members unless you explicitly declare them 'static'.
 
When the OP says "static" they don't mean in the sense of the 'static' keyword in C#, i.e. members of a class as opposed to members of an instance of a class, but rather having values that don't change.

Yes, sorry about that. I do know what static means in terms of members/fields.
 
Back
Top Bottom