You know, I find this really weird.

In C#, if you have a class

class Whatever
{
  private string unused ;
}

And you try and compile this class, of course you’ll see:

Warning 1 The field ‘Whatever.unused’ is never used

And if you assign it a value but never read from it:

class Whatever
{
  private string unused ;

  public Whatever()
  {
    unused = "blah" ;
  }
}

You get

Warning 1 The field ‘Whatever.unused’ is assigned but its value is never used

BUT, if you assign it a special value:

class Whatever
{
  private string unused ;

  public Whatever()
  {
    unused = System.Environment.NewLine ;
  }
}

The warning goes away.. . ?

WHY? I had a such unused variable that was assigned System.Environment.NewLine and I only noticed by happenstance that the variable was indeed unused. Why doesn’t the compiler flag it?

Post a Comment