Long pass-by-ref parameter lists versus wrapper types

I need to get three objects from a function, my instinct is to create a new type to return three links. Or, if the links were of the same type, I could use an array. However, pass-by-ref is simpler:


        private void Mutate_AddNode_GetGenes(ref NeuronGene newNeuronGene, ref ConnectionGene newConnectionGene1, ref ConnectionGene newConnectionGene2)
        {

        }

Obviously, there is nothing wrong with that, but I hesitate to use this approach, mainly I think for reasons of aesthetics and psychology. Are there any good reasons to use one of these approaches for others? There may be a performance issue when creating additional wrapper objects or pushing parameters onto the stack. Please note that in my particular case this is processor code. CPU cycles matter.

Is there a more elegant C # 2 approach to C # 3?

Thanks.

+3
source share
3 answers

For almost all computing tasks, you will not notice the difference in CPU. Since your sample code has the word "Gene" in it, you can fall into the rare category of code that you would notice.

Creating and destroying objects just to wrap other objects will cost a bit of performance (they need to be created and garbage collected at the end).

Aesthetically, I would not create an object to group unrelated objects, but if they logically belong to each other, it is quite normal to determine the contained object.

+3
source

( , IMHO), . 32- .NET( 64- 4.0) / , , , struct ref.

+1

, , . , . , 20% , 80% . , 20%, , , .

So, performance issues aside, I would probably use a container class. Since this method accepts only these three parameters and (presumably) changes each of them, it seems to make sense to have it as a container class method with three member variables instead of ref parameters.

+1
source

Source: https://habr.com/ru/post/1714928/


All Articles