-1

I need to port some C++ code that I wrote 10 years ago to C#. I'm not a C# expert and I've been told that using structures in C# is bad practice.

This is the original C++ structure:

typedef struct TacticalLineStruct { 
   int NumGroups;                   // The number of groups in the struct
   int GroupID[MAX_UNITS];  // which Group the vertex belongs to
   int EdgeList[MAX_UNITS * MAX_UNITS][2];  // An array of edges
   float EdgeWeight[MAX_UNITS * MAX_UNITS]; // The weight for each edge
   int NumBelowThreshold;
   POINT GroupCenter[MAX_UNITS];    // The geographical center of each group
   int NumEdges;
} TacticalLineStruct;

What is the proper way to do this in C#?

EdChum
  • 376,765
  • 198
  • 813
  • 562
zetar
  • 1,225
  • 2
  • 20
  • 45
  • 3
    Why are structs bad to start with? – Patrick Hofman Apr 04 '19 at 15:03
  • Second, this is not a code writing service. Tell us where you got stuck and then we can help you. – Patrick Hofman Apr 04 '19 at 15:03
  • 2
    VTC too broad: this highly depends on the usage. – YSC Apr 04 '19 at 15:04
  • 2
    Write it using classes, struct are usually bad practice because they are copied every type you pass them as params. – Hugo Apr 04 '19 at 15:04
  • So, just rewrite it as a class? – zetar Apr 04 '19 at 15:05
  • 3
    @Halhex If that is a problem highly depends on the usage of it. – Patrick Hofman Apr 04 '19 at 15:05
  • 1
    @zetar For this particular structure, yes please. For why, see https://stackoverflow.com/q/3942721/11683. – GSerg Apr 04 '19 at 15:06
  • @PatrickHofman Of course, no one can analyse and say for sure which implementation would be best but, in C# though, classes tend to be better practice and the "default" option in most cases. – Hugo Apr 04 '19 at 15:07
  • 1
    *I need to port some C++ code that I wrote 10 years ago to C#. I'm not a C# expert* -- Red flag time. Are you trying to do a line-by-line translation from C++ to C#? The way porting is done is to know what the program or module is supposed to do in the source language (C++), and then implement that in the destination language (C#). If you don't know C# and instead relying on knowledge of C++, you may end up with code that is inefficient, buggy, and plain looks weird to programmers who know C#. The same thing vice-versa (C# to C++). – PaulMcKenzie Apr 04 '19 at 15:14
  • 1
    The keyword 'struct' may be identical between the 2 languages, but in C++ 'struct' and 'class' are virtually identical, unlike C#. C# 'class' is the normal equivalent of either C++ struct or class. C# 'struct' is a special purpose tool that is definitely *not* intended to be the default type construct. – Dave Doknjas Apr 04 '19 at 15:25

1 Answers1

1

I have to say I'm quite surprised to see most of you agreeing on the fact that struct are a bad practice. I strongly disagree: it is just a different tool with its benefits and disadvantages like any other the language offers. Structs are value types, they work like any value (int, float, char ...) : they get passed by copy instead of by reference (in C++, it is equivalent to a function taking a parameter without using the ampersand &).

Basically, you have to know what you are going to use that object for. If you are going to send it through a large portion of your code-base, maybe consider using a class, since following a pointer is usually faster than copying the whole struct. On the other hand, if this object is supposed to live inside a function stack to process some other stuff, maybe a struct is what you need.

In any case, we can't tell you how to do your job :p We can however point you to a direction we find convenient; but we'd surely need more info than just "how do I do this?".

In conclusion, to the question "Are structs a bad practice?", I would answer absolutely not. You just have to know when to use them. For the implicit question of "how should you implement your solution, I can't say much considering you didn't ask a clear question (which is probably why your question got down-voted)

Naliwe
  • 322
  • 1
  • 8
  • Thanks. I'm certainly not asking how to do this. I was told that using a struct in C# was terrible. – zetar Apr 04 '19 at 15:21
  • It would be like to say using pass-by-value in cpp is terrible. Yes, in most cases, you want to avoid copying everything. But in a lot of other cases, you do want to copy! Maybe for thread safety, maybe for data alignement management, maybe for performances (like inside an ECS for example) etc... – Naliwe Apr 04 '19 at 15:24
  • In C#, you should start by using a class and only in special cases change that to a struct. It's definitely abnormal to assume your own types are going to work like int/bool/etc. – Dave Doknjas Apr 04 '19 at 15:28
  • I guess that works, yeah. But again, since we don't know what you want to do with that object, it's kinda hard to say. – Naliwe Apr 04 '19 at 15:32
  • @Naliwe ...or for value semantics in FP ;) – Max Langhof Apr 04 '19 at 15:43
  • @MaxLanghof Exactly ^^ But I didn't want to go there :3 – Naliwe Apr 04 '19 at 17:01