0

I have just been asked this question in an interview: "What can't be done in a nested loop in C#?" and I was stumped!

Is there a correct answer to this question? Or is it meant to stump people?

Marci-man
  • 2,113
  • 3
  • 28
  • 76

2 Answers2

2

I am almost certain that the interviewer was talking about breaking out of the outer loop without using goto statement.

This is, indeed, a problem that has been successfully solved in other similar languages, such as Java.

This Q&A provides more information on the problem.

Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Cannot guess what was expected answer but one comes to mind. It is mentioned in C# specs chapter 3.7 Scopes

within a nested block it is not possible to declare a local variable with the same name as a local variable in an enclosing block

ASh
  • 34,632
  • 9
  • 60
  • 82
  • That's perfectly fair. However, this isn't unique to blocks inside nested loops: blocks inside top-level loops, inside `if`s, etc. all fall under this restriction. – Sergey Kalinichenko Oct 20 '16 at 13:44
  • @dasblinkenlight, I fully agree with you. extereme examples `{ { int t = 0; } { int t = 1; } }` - allowed, `{ int x = 0; { int x = 0; } }` - doesn't compile – ASh Oct 20 '16 at 13:59