I'm trying to understand what the convention is for placement of C# and/or C++ structs within a project. In it's own source file? If yes, are there any conventions that I should make a habit of following?
In first year, the conventions weren't really discussed, but generally, we "stuck" the struct wherever it was being used or "felt" most relevant...
In my particular case, I have a couple of structs explicitly made up of value types which are going to be passed throughout the particular application and it's classes, so I can't definitely say that any one particular "area" can claim any sort of ownership.
Welcome to Microsoft Community. Please be informed that you can go to local disk C: drive in Windows 10 by pressing Windows + E key from the Windows that appears, on the left hand side you will see This PC click on it and all the drives will be expanded. Click on C: Drive.
Your file explorer should appear by default on your task bar; it's icon looks like a file folder. If you don't have access to that shortcut, you can just type "this pc" or "file explorer" into the search box, and to get to your C: drive, just type "c:" into the same box.
The C: drive, also known as your computer's hard drive, has the important job of storing your computer's operating system (Windows, Mac OS, Linux, etc.), as well as applications you use (e.g. Microsoft Office, Adobe, Mozilla Firefox) and files you download from the internet.
A small struct that relates solely to a single class is often put in the same file as that class. Similarly, a small simple class that is used within a single class is often put in the same file. This identifies that the item relates to a specific class and is means of grouping it with that class.
Any struct that isn't both simple (has only a handful of values) and specific to a single class should be in its own file.
In C++, if the struct is "owned" by a certain class or interface or the like, then I prefer to declare it as an inner type to what owns it. If the struct is a common protocol that many different classes make use of, then I put it in its own .h file. If the struct is one of a related suite of small-ish structs, then I make a namespace for such things and declare all the structs within a single .h file that provides that namespace.
In C# I would probably do the equivalent as in C++.
You should be able to just create a separate file for each struct or each group of related structs (I'd recommend one per struct, but sometimes that gets tedious - if you group them make sure to name it thoughtfully).
If the project is large enough to have "areas" as you call them, then it would probably be smart to introduce one or more namespaces for the structs that appropriately describe them. This will prevent naming collisions and give your code-using clients a way to remember what's where. Having to qualify names can seem painful sometimes, but its important in large projects and does help in the end.
Lastly, most larger projects have a Common folder in either a high level directory or in the location where their message interfaces are defined. This is important so everyone can recognize that location holds data structures which should be used by all to keep a clear, consistent interface to avoid the need for lots of conversion code which can be time consuming and costly (and usually ends up being replicated too!).
If the structure is used across multiple classes, I put it in its own file. Stylecop doesn't complain either way. And reshaper has a re-factoring to move a struct to its own file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With