4

The whole program in a web form of a .net web-application:

namespace WebApplication1
{
   public partial class WebForm1 : System.Web.UI.Page
   {
     protected void Page_Load(object sender, EventArgs e)
     {
        string x = "";
        string y = String.Empty;
     }
   }
}

If I build the application, the compiler underlines x,

The variable x is assigned to but it´s value is never used

For y, I get no underlining. Why not? (VS 2008, .Net 3.5)

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
  • 1
    possible duplicate of [C# Compiler should give warning but doesn't?](http://stackoverflow.com/questions/2740885/c-sharp-compiler-should-give-warning-but-doesnt) – Eric Lippert Feb 01 '12 at 16:28

3 Answers3

10

I answer your question in detail here:

https://stackoverflow.com/a/2741281/88656

Briefly: the compiler detects that the local is written to but not read during the flow analysis pass. It deliberately suppresses the warning if the value written to the local is a non-constant. String.Empty is not a constant, it is a read-only field, oddly enough. But the empty string literal is a constant. That is why you see the warning for the one with the literal but not for the field.

The compiler is reasoning that you might be assigning the value of the expression to an unread-from local in order to facilitate debugging the program. We do not want you to have to turn "warnings are errors" off every time you introduce an explanatory variable to assist in debugging. The fact that in this case, obviously you are not using the variable to examine the output of String.Empty is lost upon the compiler; it does not know what the semantics of the field reference are.

Community
  • 1
  • 1
Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
  • "String.Empty is not a constant, it is a read-only field, oddly enough" is this a design decision or a mistake? – Lukasz Madon Feb 01 '12 at 18:13
  • 1
    @lukas: This is a question-and-answer site; if you have a question, post a question! But in this case, it already has been posted many times. http://stackoverflow.com/questions/507923/why-isnt-string-empty-a-constant – Eric Lippert Feb 01 '12 at 18:20
  • Why does the compiler reason that I use the variable in order to facilitate debugging? I personally, like String.Empty, because it makes the program just much more readable. And I even more like to get a warning of an unused variable because this happens very frequently. – AGuyCalledGerald Feb 01 '12 at 18:51
  • 2
    @AGuyCalledGerhard: The answer to your question is "because our customer feedback and personal experience indicates that this is a common pattern.". You are entirely entitled to your personal preference. If you want to get warnings for unused local variables that are assigned non-constant values, consider using Resharper, FXCOP or any number of other tools that perform this analysis for you. If this subject interests you, you might read this article from 2007: http://blogs.msdn.com/b/ericlippert/archive/2007/05/15/should-we-produce-warnings-for-unused-uninitialzied-internal-fields.aspx – Eric Lippert Feb 01 '12 at 19:23
4

the compiler underlines x,

No, the compiler does not. The editor underlines. The compiler has no output channel capable of drawing a line.

For y, I get no underlining. Why not?

Because it is used?

Do not get me wrong, but without the complete code relevant by variable scope there simply is no way to say whether this is like that or not.

The whole program:

Lie. This is not the whole program ;) It is not even the complete class. And you can not have a variable in C# outside of a class.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
TomTom
  • 61,059
  • 10
  • 88
  • 148
  • 1
    @Jan-FrederikCarl: It's not the whole program that gets compiled, certainly. A short but complete program would definitely help. – Jon Skeet Feb 01 '12 at 08:38
  • 4
    Re: "The editor underlines" -- this is a hairsplitting distinction. **The compiler tells the editor what to underline**. You might as well say that no, the editor doesn't do the underlining, the windows graphics routines do the underlining, and no, they don't do the underlining, the pixels on the monitor do the underlining. – Eric Lippert Feb 01 '12 at 16:30
  • 3
    Re: "without complete code there is no way to say" -- I assure you this is false. There is more than enough information here to diagnose the symptom. – Eric Lippert Feb 01 '12 at 16:31
  • 1
    And besides **how do you know what output channels the compiler has**? I don't recall seeing your name on any of the Language Analysis Framework checkins that I reviewed when we built the channels by which the compiler and editor communicate. That system is an implementation detail; we do not publically document its mechanisms. (This of course will change with Roslyn; the communications between the core language analysis service and the services provided by the editor will be public and documented.) – Eric Lippert Feb 01 '12 at 16:42
  • Simple - common sense and intelliggence. THe COMPILER is a tool that can run from the comand line. When you look at the outbut pane in the toolbox you can see the output. Visual studio parses this information and draws the line, but it is not the compiler. – TomTom Feb 01 '12 at 17:30
  • 3
    The *command line compiler* is, as its clever name indicates, a tool you can run from the command line. It is just a thin shell around the actual compiler. The interaction between Visual Studio, the command line compiler, the language service consumed by the editor, and the compiler that produces the information about error squiggles is complex, has changed over time, and is an implementation detail. – Eric Lippert Feb 01 '12 at 17:53
  • 4
    Think about it this way: what does your "common sense and intelligence" tell you about other code analysis features in the editor? For example, when you are typing, lots of stuff is happening: syntax is being colourized, lexical, grammatical and semantic analysis errors are being produced, IntelliSense completion dropdowns and method signature tips are being produced. Are all of those done by "the command line compiler"? – Eric Lippert Feb 01 '12 at 18:10
-2

If I have understood the article I read rightly, the compiler does not know what String.Empty is because it is a read-only field. String.Empty is assigned at runtime. So I get no warning when building the program.

The article

AGuyCalledGerald
  • 7,882
  • 17
  • 73
  • 120
  • 1
    What you've said is true, but fails to provide a real answer to your question, You've successfully differentiated `String.Empty` from `""`, but the error *should* still apply, since the error would be making a true statement: "`The variable 'y' is assigned to but its value is never used`" still applies. See http://stackoverflow.com/a/2741281 . – Brian Feb 01 '12 at 17:09