'In modern c#, is there a pattern for a struct to replace a long list of arguments?
So, I want to replace a long list of arguments with a struct.
My motivation (but not my question) is: I want to reduce the amount of arguments used to pass this exact data around, without having to introduce a new object to the garbage collector.
So my idea is that i create a ref struct, and pass that on. The struct will be passed to methods that use the 'in' keyword. And because every member is now readonly, no defensive copies of this struct will happen, and all the arguments will live at exactly one place in the memory stack, and be cleaned when they fall out of scope.
This seems to me like a good, general way to replace a long list of arguments. But I might be wrong, and the fear of "defensive copies" itches in the back of my mind.
Is there a good reference, or example I can follow, in case this was not it?
readonly ref struct MappingButtonData {
readonly public AudioSourcePool sourcePool { get; }
readonly public InputCollisionManager inputCollisionManager { get; }
// ... etc
readonly public string hoverSound { get; }
public MappingButtonData ( /* each member's initial value */ ) {
this.sourcePool = sourcePool;
this.inputCollisionManager = inputCollisionManager;
// ... etc
this.hoverSound = hoverSound;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
