I am wondering if anyone has any insights on the performance benefit (or
detriment) of declaring local variables instead of referencing members.
Is allocating memory for a new variable more efficient than repeatedly
referencing the member in a loop?
Maybe using a string isn't the best example, but hopefully you get the
idea!
* example (referencing member):
String s = "this is a test";
for (int i=0; i<s.Length; i++) {
if (s.Chars[i] == 'x') {
Console.WriteLine(s.Chars[i]);
}
}
* example (creating new variable):
String s = "this is a test";
char[] chars = s.Chars;
for (int i=0; i<s.Length; i++) {
if (chars[i] == 'x') {
Console.WriteLine(chars[i]);
}
}