If I have this code:
foreach (Char c in myString.ToLowerInvariant())
{ /* code */ }
How many times will myString.ToLowerInvariant() get called? Once (which I assume) or multiple times?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Short answer: Once
Long answer:
The code gets compiled into the following IL. You can try it yourself by compiling the C# file and then opening it in ILDASM (distributed with Visual Studio) or .NET Reflector (which can show the disassembled code in many languages and has tooltips for IL instructions with detailed description).
The actual loop condition is checked on lines L_0021 to L_002c and then there a jump at line L_002e which is executed if not all characters are processed yet. Note that it jumps to L_0013 which is after the ToLowerInvariant call.