G
Guest
Hi,
I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.
It was something like this:
Consider the following code:
int i = 0;
Console.WriteLine("The value is: " + i);
Console.WriteLine("The value is: " + i.ToString());
Which would you use to write an integer to the console and why would it give
better performance?
I said I would use: Console.WriteLine("The value is: " +
i.ToString());
But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is required
from the integer for the line not using the ToString() method and generate
the same MSIL.
Can anyone please enlighten me?
Best Regards,
Steve
I recently had an interview where I was asked a number of questions on C#.
Unfortunately I didn't get the answers from the test and find that one of
them is still niggling me.
It was something like this:
Consider the following code:
int i = 0;
Console.WriteLine("The value is: " + i);
Console.WriteLine("The value is: " + i.ToString());
Which would you use to write an integer to the console and why would it give
better performance?
I said I would use: Console.WriteLine("The value is: " +
i.ToString());
But I can't think why this should give a performance advantage. I can only
guess that the compiler should be able to realise that a string is required
from the integer for the line not using the ToString() method and generate
the same MSIL.
Can anyone please enlighten me?
Best Regards,
Steve