was wondering which of the following options is more efficient. Any suggestions?
Listing 1
string header = "Header 1"; // create a string variable and reuse
client.AddMessage(header, ...);
client.AddMessage(header, ...);
client.AddMessage(header, ...);
client.AddMessage(header, ...);
client.AddMessage(header, ...);
...
Listing 2
client.AddMessage("Header 1", ...); // hard code the string in each call
client.AddMessage("Header 1", ...);
client.AddMessage("Header 1", ...);
client.AddMessage("Header 1", ...);
client.AddMessage("Header 1", ...);
....
You should probably not care about this kind of (possible) micro-optimization : what matters, here, is maintenability :
(The compiler should optimize that for you, anyway, I suppose)
Strings are interned in the .NET world, so either one will work the same way.
In other words - it makes no difference in regards to performance.
As for maintainability - if you need to change the header name, option 1 is better (DRY).
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