-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#?

10
  • 3
    Why are structs bad to start with? Commented Apr 4, 2019 at 15:03
  • 2
    VTC too broad: this highly depends on the usage. Commented Apr 4, 2019 at 15:04
  • 2
    Write it using classes, struct are usually bad practice because they are copied every type you pass them as params. Commented Apr 4, 2019 at 15:04
  • 3
    @Halhex If that is a problem highly depends on the usage of it. Commented Apr 4, 2019 at 15:05
  • 1
    @zetar For this particular structure, yes please. For why, see stackoverflow.com/q/3942721/11683. Commented Apr 4, 2019 at 15:06

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I'm certainly not asking how to do this. I was told that using a struct in C# was terrible.
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...
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.
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 ...or for value semantics in FP ;)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.