0

I'm developing with C#, Visual Studio 2017 and .NET Framework 4.7.1.

My program do a lot of calculations, and I don't want to create new Collections to store them. So, I have created a collection to reuse it (the only thing that I'm going to do is change the collection's content):

List<List<double>> offsprings;

Inside the following method I will clean the contents of offsprings parameter to not create new List<double>:

public void GlobalRecombination(
    List<List<double>> population,
    List<List<double>> offsprings,
    int numOfObjectVariable,
    int numOfStrategyParameters,
    RecombinationType objVarRecomType,
    RecombinationType straParamRecomType)
{
    foreach (List<double> offspring in offsprings)
    {
        offspring.Clear();

        for (int i = 0; i < numOfObjectVariable; i++)
        {
            double newValue = 0.0;

            [ ... ]

            offspring[i] = newValue;
        }

        int total = numOfObjectVariable + numOfStrategyParameters;
        for (int i = numOfObjectVariable; i < total; i++)
        {
            double newValue = 0.0;

            [ ... ]

            offspring[i] = newValue;
        }
    }
}

My question is: Which is the best way to return offspring parameter?

Now, the method works, but I'm not sure if this is the right way to do it.

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 1
    Possible duplicate of [What's the difference between the 'ref' and 'out' keywords?](https://stackoverflow.com/questions/388464/whats-the-difference-between-the-ref-and-out-keywords) – ste-fu Jul 26 '18 at 10:52
  • 1
    I'm going to go with "neither". You have a class with a field here that's struggling to get out. – Jeroen Mostert Jul 26 '18 at 10:54
  • "Inside the following method I will clean the contents of offsprings parameter to not create new List" And what is the purpose *behind* this? You are aware that by clearing the original list and adding new items you´re effectivly creating a new list anyway. So if you really want to share the same reference you don´t have to do anything, as all your updataes to the list are reflected to all references to it. – MakePeaceGreatAgain Jul 26 '18 at 10:55
  • @HimBromBeere Have you seen the code? I'm not creating anything new and I'm not destroying any object. – VansFannel Jul 26 '18 at 10:56
  • 1
    Do you want to return `offspring`( a single element), or `offsprings` (the list)? – MakePeaceGreatAgain Jul 26 '18 at 10:57
  • @HimBromBeere I have posted the code to show you what I'm doing. – VansFannel Jul 26 '18 at 10:58
  • @ste-fu I'm not asking about what is the different between ref and out. This is not a duplicate question. – VansFannel Jul 26 '18 at 11:01
  • Your code is working and borderline acceptable. Changing the contents of a parameter container is unwise and probably a negative optimization. Putting that parameter at position 2 and nor marking it in the name in any way is a bad design. – bommelding Jul 26 '18 at 11:06
  • 1
    There´s no simple answer, it depends on what clients of your API do with those lists. As you seem to completely rebuild your data-structure I´d consider to indicate this at least via some documentation or even better via the methods name. `ref`- and `out` may or may not be a solution, depending on the context. Having said this your question is quite borad and opinion-based. – MakePeaceGreatAgain Jul 26 '18 at 11:36

0 Answers0