Skip navigation

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?

Advertisements

2 Comments

  1. happened to me also with string list and int i =0;

  2. Perhaps because the code being accessed from the other class could have functional side effects … so the compiler is forced to assume it’s valid. This is just a guess.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: