-1

The Microsoft documentation describes it as:

Variables are either initially assigned or initially unassigned. An initially assigned variable has a well-defined initial value and is always considered definitely assigned. An initially unassigned variable has no initial value.

But what does this actually mean?

Is it simply stating when a variable is declared it's either assigned a value (i.e., such a variable is an initially assigned a value) or not (i.e., such a variable is initially unassigned).

Is my assumption correct?

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77

1 Answers1

1

Yes it is just a statement. There are not any major differences between the two. Unassigned variables are initialized with the default value for the data type when the program runs. The only time you will run into an issue with unassigned variables is if you assign it in if blocks and don't have an else statement and then attempt to use the variable outside the scope of the if block. The compiler will throw an error saying that the variable is potentially unassigned in this case. Ex:

string testString;
if (some condition here)
    testString = "Success";

if (testString == "Success") //This line will throw an error because testString may be unassigned
string testString = "Failure";
if (condition)
    testString = "Success";

if (testString == "Success") //No error this time becuase testString was initialized with a value
string testString;
if (condition)
    testString = "Success";
else
    testString = "Failure";

if (testString == "Success") //No error here because all paths will assign a value to testString

Edit from Microsoft:
Initially assigned variables
The following categories of variables are classified as initially assigned:

  • Static variables.
  • Instance variables of class instances.
  • Instance variables of initially assigned struct variables.
  • Array elements.
  • Value parameters.
  • Reference parameters.
  • Variables declared in a catch clause or a foreach statement.

Initially unassigned variables
The following categories of variables are classified as initially unassigned:

  • Instance variables of initially unassigned struct variables.
  • Output parameters, including the this variable of struct instance constructors.
  • Local variables, except those declared in a catch clause or a foreach statement.
public static int globalId; //assigned
public class TestClass
{
    int Id; //Assigned
    public void TestMethod()
    {
        int methodId; //Not assigned
    }
}
Wellerman
  • 846
  • 4
  • 14
  • 1
    To add on to your answer. When you say "Unassigned variables are initialized with the default value for the data type when the program runs." Are you basically saying initally unassigned variables are automatically initialized with the respective default value. While a initially assigned variables are manually intialized(by the programmer) with the respective custom value? –  Apr 17 '21 at 04:01
  • Yes, for example a `string` is given the value of `null` in memory when it is unassigned – Wellerman Apr 17 '21 at 04:05
  • 1
    @Oakley Big thanks for the help so far. However to continue, the Microsoft documentation also stated what variables are classified as intially assigned and what variables are classified as intially unassigned(see doc)... –  Apr 17 '21 at 04:21
  • 1
    In doing so, is it basically stating certain variables(classified as initially assigned variables) should be assigned with an initial value when declared and while the others(classified as initially unassigned variables) should not. Or is it stating certain variables(classified as initially assigned variables) are automatically intialized with a default value and while the others(classified as initially unassigned variables) are not are not. Or do I have this part all wrong??? –  Apr 17 '21 at 04:22
  • See my above edit. This is more related to where/how the variable is used than the data type. The easiest way to explain this is the difference between static and instance variables. For example `static int Id;` is initialized to `0` and `int Id;` is not initialized. Instance variables of a class are assigned though (variables declared within a class but outside of a method) – Wellerman Apr 17 '21 at 04:39
  • From a performance perspective though, there is no difference in behavior between the initially unassigned and initially assigned variables. – Wellerman Apr 17 '21 at 04:40
  • 1
    so than would static variable Id be considered initially assigned while instance variable Id be considered initially unassigned? –  Apr 17 '21 at 04:44
  • Yes, that is correct – Wellerman Apr 17 '21 at 04:45
  • 1
    But then again from what I know, according to that same microsoft document, it states that both static and instance variables are automatically intialized with the respective default value if not manually initialized by the programmer. Thus, would not both static and instance variables be considered initially assigned variables? –  Apr 17 '21 at 04:47
  • 1
    @Oakley Just to reiterate my assumptions with all that we have just discussed: An initially assigned variable refers to a variable that when first declared was either automatically initialized with the respective default value(because such variable could be) or was manually initialized by the programmer. While an initially unassigned variable refers to a variable that when it was first declared it was neither automatically initialized with the respective default value(because such variable could not be) nor was it manually initialized by the programmer. –  Apr 17 '21 at 05:12
  • Yes, this is correct. It is typically considered best practice to declare and assign variables as needed though, I find it very rare to need to code an unassigned variable. Some devs like to just declare all their variables at the top of a method and assign them as needed, but this makes the code hard to read. Keep this in mind when coding, and assign variables as you declare them whenever possible. – Wellerman Apr 17 '21 at 05:17
  • 1
    Big thanks for the help. Initially I had a totally different understanding with initially assigned and initially unassigned variables. But this sure cleared up a lot of questions I had. –  Apr 17 '21 at 05:28