SDLWeb
SDLWeb

Reputation: 1

C# Method Calling Several times

I have a method GetNames() that returns comma separate strings from an array. The array contains a list of names. I am calling this method in my ViewModels three times. Will it make any performance difference between one call one time and store that return values in an object. Or 2. by calling that method each time.

1.

string nameParm = GetNames();
_metaController.GetNamedetails(nameParm); //Some operation
_metaController.UpdateNamedetails(nameParm, "Approved");

2.

_metaController.GetNamedetails(GetNames()); //Some operation
_metaController.UpdateNamedetails(GetNames(), "Approved");

Upvotes: 0

Views: 294

Answers (1)

Twenty
Twenty

Reputation: 5871

I am not sure, if I understand your question correctly, but yes it will make a 'performance difference'. See, every time the line GetNames() will be written, it will execute the method.

Assuming you have a console app, you can easily verify this by writing Console.WriteLine("Method hit!") in your GetNames method. If you start your project, you will see that you have Method hit! two times in a row.

You could also check for such a behavior by setting a break point in your method and debugging your project.

So you would be better of using your first method 99% of the time.

Upvotes: 1

Related Questions