What sets a null variable?

Next, I get a compile-time error that says: "Using an unrecognized local variable" match "if I just enter a string match; but it works when I use string match = null; So, what is the difference, and if the string is not assigned value immediately, I have to assign zero, how is it?

string question = "Why do I need to assign to null"; char[] delim = { ' ' }; string[] strArr = question.Split(delim); //Throws Error string match; //No Error //string match = null; foreach (string s in strArr) { if (s == "Why") { match = "Why"; } } Console.WriteLine(match); 
+7
source share
6 answers

C # language prohibits the use of local until it is finally assigned a value. In this example, the compiler does not understand the semantics of Split and should assume that strArr may be an empty collection and, therefore, the body of the loop could potentially not be executed. This means that from the point of view of the final assignment, foreach does not assign a value to match . Hence it is still not assigned when you get to WriteLine

By changing the declaration to string match = null , the value will be marked as definitely assigned from the very beginning. Therefore, loop calculation does not matter.

+12
source

Depending on your scenario:

 string match = null; 

Or:

 string match = string.Empty; 

are acceptable practices.

In your case, it is impossible to assign a value for match , so a compiler error.

+5
source

You find the difference between a declaration and an assignment. Declaration with lines like

 string match; 

just announces to the compiler that you will use the match variable of the type string. Destination with strings such as

 match = null; 

assigns a null match value.

A language can declare that declarations and assignments should always be separated (I'm not 100% sure, but I believe that older versions of Visual Basic did this), but most languages ​​allow you to combine declarations and assignment

 string match = null; // combined declaration and assignment 

means

 string match; // declaration match = null; // assignment 

C # requires variables to be assigned before they are used. Unlike fields and events, local variables are not automatically assigned to default values, so you need to prove to the compiler that before using match value will have some value. The compiler does not care what the value of match is if this variable has a type string.

In your case, the compiler cannot prove with local analysis that strArr will be nonempty because the compiler does not check the Split code, therefore there is no guarantee that the code will even go into foreach , let's match assignment condition. Because calling Console.WriteLine uses match , and since match cannot be assigned at run time with a string match declaration, the compiler requires you to assign match outside the loop. One way to satisfy this requirement is to use string match = null instead of string match .

+4
source

The compiler realized that there is a chance that you can use match if it is never tied to anything. The foreach never be executed. Thus, you declared a variable, but the compiler realized that it can be accessed without being assigned, therefore, an error.

+2
source

You have an if () block that initializes the match variable if the condition is met. In this case, the match is an object representing the actual block in memory.

However, if the if () condition is not met, there is no "else" block that initializes the default variable "match", in which case you will try to access the uninitialized object, which will fail.

you can get around this:

  • As you commented, the default is to match before the for loop.
  • Adding a default else condition after a for loop.

Fortunately, if you are working on an IDE, this points to this as an exception to compilation.

+1
source

When you specify, type variable = null; , you initialize the variable. If you specify type variable; , you only declare a variable.

0
source

All Articles