4

A silly but probably not-so-uncommon error when I replaced a string literal by a static string was leading to something like the following code snippet which is probably almost never intended and trivial to spot, even for a machine:

public partial class T { static string S = S; }

This doesn't trigger a diagnostic with VS Professional 2013, .NET Framework 4.6.0.1055, Warning level 4.1 Why not?


1To be fair with the compiler I assume that the initialization is well defined and assigns S' value in its default value state prior to the explicit initialization, i.e. null, to itself when the initialization is eventually performed; but it is almost certainly a coding error.
Peter - Reinstate Monica
  • 15,048
  • 4
  • 37
  • 62

1 Answers1

6

In recent versions of Visual Studio it says:

Warning CS1717 Assignment made to same variable; did you mean to assign something else?

So... update your IDE :)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Marc, I apologize -- I cannot reproduce my scenario with my version now and get the same warning as you do with VS2013, too. I don't want to undo all the changes since, so I cannot say what the difference is. There is a small but non-zero possibility that I spaced and had assigned from a similar but different static variable. – Peter - Reinstate Monica Apr 26 '16 at 21:52