Explicit assignment of null

string s1; string s2 = null; if (s1 == null) // compile error if (s2 == null) // ok 

I really don't understand why explicit assignment is required. What is the difference between a null variable and an unassigned variable? I always assumed that unassigned variables were just assigned as null at runtime / compiler. If they are not equal to zero, then what are they?

+50
c #
Oct 08 2018-10-10
source share
7 answers

Unassigned elements are automatically initialized to default values โ€‹โ€‹(which is a null reference in the case of string ).

Unassigned local variables are not assigned any values, and trying to access a possibly unassigned variable will give a compilation error.

+73
Oct 08 2018-10-10
source share

The reason why an explicit assignment is required is pretty simple. This is often a source of error when people try to use unassigned / uninitialized variables.

By forcing the developer to do this, he eliminates the errors that occur when the developer forgets to initialize the variable. And by initializing it, you control it.

This is really good! I donโ€™t know how often I had uninitialized or undefined variables in some scripting languages โ€‹โ€‹that took quite a while to find ^^

+12
Oct 08 '10 at 12:00
source share
  • If you have an unassigned local value, you are most likely doing something stupid. The worst part is that you are doing such a stupid thing that smart people can do in the midst of the moment (doing something stupid every day).

  • Unlike some of the things that lead to warnings, there is no possible advantage of using an unassigned value in a particular remarkable case.

  • The only difference in cost between a valid local or supposed specific value is a few keystrokes (usually = null; no more than = default(SomeType);

The prohibition of such designs is difficult for professionals and low levels of cons. There are no technical reasons why the language could not be designed to allow unassigned locals, but the advantages of the ban outweigh the disadvantages.

+7
Oct 08 2018-10-10
source share

The C # compiler does not allow the use of uninitialized local variables. An initially unassigned variable has no initial value.

+4
Oct 08 2018-10-10
source share

Take a look at the specification: 5.3 Specific purpose

A specific purpose is a requirement in the following contexts:
A variable must be definitely assigned in every place where its value is obtained.

s1 and s2 are not initially assigned ( 5.3.1 Initially assigned variables ), but only s2 is considered to be definitely assigned in this place, [because] all possible execution paths leading to this location contain at least one of the following:

  • A simple assignment (section 7.13.1), in which the variable is the left operand.

As you can see, null in this context does not matter. Purpose is important, but not value.

+4
08 Oct 2018-10-10
source share

A variable that is never assigned contains the value undefined . In the worst case, what actually happens in most languages, this means that it can have any meaning , since it refers to some part of the memory that was most likely previously used for another purpose, potentially even a different program. Some languages โ€‹โ€‹ensure that all variables are initialized with some reasonable defaul values. However, this may just be a waste, since in the end it is a write operation that may not be required, doing it by default is a waste of time.

+2
Oct 08 2018-10-10
source share

One thing to keep in mind is that the area will play a role in this. If you defined S1 as a class variable, then you checked it inside a function that the compiler would not stop, and the code would work fine. The reason is that the variable is initialized when the class is instantiated.

Moving inside the method, and there is a good chance that you forgot something when testing a variable before initializing it.

Another caveat I see is that the default string is? (And even more importantly, this is in the specification, which will not change? Keep in mind that the empty string does not match the zero string. There is a way around this, although you can check with string.IsNullOrEmpty (S1) instead.

0
Oct 9 2018-10-09
source share



All Articles