1

Is there any difference between these declarations?

 Animal animal;  
 Animal animal1 = null;
3
  • 2
    Yes. First one you are expecting the compiler to set the object to its default value and in the second object you are explicitly setting it's value to null which is always a safer approach over the first one.
    – Baahubali
    Jun 23, 2016 at 5:02
  • 5
    msdn.microsoft.com/en-us/library/edakx9da.aspx. In summary, the first form will generate a compiler error if you attempt to access the variable before assigning it, the second will generate a runtime error.
    – Claies
    Jun 23, 2016 at 5:03
  • 1
    Are those local variable declarations or field declarations? Jun 23, 2016 at 6:36

2 Answers 2

6

There is a difference according to C# specifications based on the context. In a context of a method, the compiler can prove that the first animal is not assigned, and so can generate a compiler error on the next line where it is used. C# compiler will not allow you to use a method variable if the compiler can prove that is was not assigned.

However, if animal is a Class Field it will be automatically initialized to the default value and the compiler will not generate an error.

Below is the C# Specification:

5.3 Definite assignment

At a given location in the executable code of a function member, a variable is said to be definitely assigned if the compiler can prove, by a particular static flow analysis (§5.3.3), that the variable has been automatically initialized or has been the target of at least one assignment.

So for the first, the compiler generates a "Use of an unassigned local variable". While for the second, you are explicitly assigning the variable to null, so the compiler should be OK with that.

0
4

No, there isn't any significant difference. At the end, on the runtime, you will have a variable called animal of type Animal, whose value would be null at the point of definition and later on you would set logically it's value to a non null value, in order to use it.

However, some would opt the first rather than the second declaration and vice versa. Furthermore, some would argue that the assignment of null in the second case is redundant. For instance, if you use ReSharper, I think that you would noticed a message about the redundancy of setting there the null.

Last, but not least I suppose that you define a variable of type Animal and later on you try to set it's value (I refer to the first case). If that's the case, you are in a good state. Otherwise, If you had the following snippet:

Animal animal;
if(animal==null)
{

}

You could get a warning before the compilation and a compilation error with the following description:

Use of unassigned local variable 'animal'

If you had declared only the variable and you had never used it, you would have gotten a warning with the following description:

The variable 'animal' is declared but never used

All the above refer to the case of a local variable, if we were in the case of a class variable then the things are a bit differently. If you create a console application and you have the following snippet:

class Program
{
    static Animal animal;

    static void Main(string[] args)
    {
        if(animal==null)
        {

        }
    }
}

You will not get any compilation error. You will get only a warning with the following description:

Field 'Program.animal' is never assigned to, and will always have its default value null
4
  • 1
    As noted above by Claies, the fact that you explicitly assign a null value in the second form makes the compiler think you intended for this, thus it won't see it as an error if you try to use it before assigning a value. Jun 23, 2016 at 5:19
  • @DrunkenCodeMonkey I will add also this in my post thanks
    – Christos
    Jun 23, 2016 at 5:23
  • well explained.
    – Rajdeep
    Jan 17, 2017 at 4:33
  • @Spark Thank you :)
    – Christos
    Jan 17, 2017 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.