Resharper recommends that these vars:
List<string> senderDeviceIDList; string senderDeviceID; . . . foreach (var item in PlatypiIds) { senderDeviceIDList = await GetSenderDeviceIDForSenderID(item); senderDeviceID = senderDeviceIDList[0];
...can be declared in inner scope, like so:
foreach (var item in PlatypiIds) { List<string> senderDeviceIDList = await GetSenderDeviceIDForSenderID(item); string senderDeviceID = senderDeviceIDList[0];
...but is that really "more better"? Doesn't that cause the vars to be declared N times (once for each foreach loop)?
The var statement declares a function-scoped or globally-scoped variable, optionally initializing it to a value.
When the variable scope is available only within the method where it is defined, then that variable is called as a local variable.
There is no any benefit in terms of performance or memory allocation here, as variables inside or oustside of the if
scope are declared in the IL
, by the way.
The only benefit is localization of the variable scope. Move it to the scope where it's used, which brings goodness like:
easy refactoring (may be the most important)
readability. If you see variable inside the scope, you know that its used only inside that scope and if you see some variable that apparently is not inside, you know that changing it can affect other parts of the code. So changing it introduce some potential danger.
In short, it's about readability and usability of the code you're writing and does not introduce any performance or memory consumption benefits.
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