简体   繁体   中英

Why does this program only produce three warnings?

public class Program
{
     public static void main(String[] args)
     {
         string message = "This is a message";
         int number = 6;
         object obj = null;
         int? nullable = (int?)12;
     }
}

The first three variable declarations on this program throw the following warning:

The variable 'X' is assigned but its value is never used

Yet, the last statement:

int? nullable = (int?)12;

doesn't throw anything. Why is that?

在此输入图像描述

The warning that you're seeing is only shown in cases where the compiler can prove that the expression used to initialize the variable can't possibly cause any side effects. When you're just assigning a literal string, integer, or null value to a variable, the compiler knows that none of those things can possibly cause side effects. For your last value you're not just assigning a literal value though; you're using the explicit operator of a type as well, and as far as the compiler is concerned, that operator is just some code that could do anything . It could, for example, cause relevant side effects (it doesn't, but the compiler doesn't know that) that would make the line not superfluous.

Warnings are best practice suggestions, they are not true errors. Visual studio is smart enough to see you created the variables but never used them so they are useless. It is recommending that you get rid of them since you don't use them.

If you actually do something with them then the errors will go away. For example if you said:

number += nullable;

Would get rid of 1 of the errors. If you did something like:

message = message + " and this is more message";

It would get rid of the other error.

I believe it sees the int? as an object and since you are casting it to the nullable variable, it can't figure out if it had been used before hand. I think it has something more to do with the casting just isn't caught as an error because it can't tell if you referenced that variable somewhere else.

I think it would be the equivalent of something like this:

var a = new SomeClass();
var b = a;

Since it can't tell if a has really been used, then it doesn't show an error. If you put that in with a real class it will not show the error also.

It seems like the Warning doesn't show because the line with the nullable actually do an operation before the assignation. As for exemple the following code only generate a warning on the variable named number. This seems to be logical in the meaning that the line with warning are truely useless in the current code. The other lines might do something during the execution and will not be "optimized out". To test it run your code in release mode with the debugger ad you will see that all lines that have a warning are skipped ("optimized")

class Program
{
    static void Main(string[] args)
    {
        string message = ';'.ToString();
        int number = 6;
        object obj = (object)(new t());
        int? nullable = (int?)12;
    }
    class t
    { }
}

With this being said. The nullable it not optimized because there is a cast from a int '12' to a int?. Since it is a real cast and not a "useless cast" an operation is needed during the run time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM