Fixing the 'Use of unassigned local variable' with a null assignment. Why?

后端 未结 4 1920
滥情空心
滥情空心 2020-12-18 17:24

With a piece of code like this, the compiler complains on c.MyProperty:

MyClass c;

try { throw new Exception(); }
catch (Exception) { }

c.MyPr         


        
相关标签:
4条回答
  • 2020-12-18 18:10

    This is because the C# Language specification v. 4.0 section 1.6.6.2 "Method body and local variables" states the following:

    A method body can declare variables that are specific to the invocation of the method. Such variables are called local variables.

    [skipped]

    C# requires a local variable to be definitely assigned before its value can be obtained.

    This is to avoid letting you to shoot yourself in the foot, as Binary Worrier neatly pointed out.

    0 讨论(0)
  • 2020-12-18 18:21

    The reason for this exception is that you've not assigned a default value for the variable e.g

    if (Request.Files != null  && Request.Files.Count > 0)
                {
                     Image = Request.Files.AllKeys[0];
                }
    var stream  = new FileStream(Image,FileMode.Open);
    

    now the Image variable will give compiler error

    Use of unassigned local variable 'Image'

    That is due to the reason that there is possibility that condition becomes true and control will never get to know what the Image variable is . so either place an else block or assign a default value as below.

    string Image = "";
    if (Request.Files != null  && Request.Files.Count > 0)
                {
                     Image = Request.Files.AllKeys[0];
                }
    var stream  = new FileStream(Image,FileMode.Open);
    
    0 讨论(0)
  • 2020-12-18 18:29

    When you assign null to the variable you're telling the compiler to back off because you know better than him so he should not complain about this.

    This is probably due to the fact that assigning null is considered to imply an explicit action by the developer.

    0 讨论(0)
  • 2020-12-18 18:31

    Think of it this way, the compiler is not telling you that it is going to throw a null reference and therefore it can't compile, but rather that one of the conditions aren't met that are required to compile, i.e. that it has to be definitely assigned.

    According to the spec null is a c# literal: "The null-literal can be implicitly converted to a reference type or nullable type"

    As well as in terms of assignments: (taken from the spec)

    Start of quote taken from the spec

    5.3.3 Precise rules for determining definite assignment In order to determine that each used variable is definitely assigned, the compiler must use a process that is equivalent to the one described in this section.

    The compiler processes the body of each function member that has one or more initially unassigned variables. For each initially unassigned variable v, the compiler determines a definite assignment state for v at each of the following points in the function member:

    · At the beginning of each statement

    · At the end point (§8.1) of each statement

    · On each arc which transfers control to another statement or to the end point of a statement

    · At the beginning of each expression

    · At the end of each expression

    End of quote

    So even though null is not actually pointing to an object in memory, it does fulfill the requirements of being definitely assigned and that is why the compiler allows it.

    0 讨论(0)
提交回复
热议问题