Possible Duplicate:
Difference between declaring variables before or in loop?
I was asked following question in one of my interview. Which loop is more efficient
void Fun()
{
string strTest = string.Empty;
for (int i = 0 ; i < 10000; i++ )
{
strTest = "String : " + i;
System.Console.WriteLine(strTest);
}
}
OR
void Fun()
{
for (int i = 0 ; i < 10000; i++ )
{
string strTest = string.Empty;
strTest = "String : " + i;
System.Console.WriteLine(strTest);
}
}
I think declaring string outside is more efficient? Am I right?
I was asked why - I told it will create lees overhead on GC.
Please correct me if I am wrong.
Regards,
Hemant Shelar