Initialising


When a procedure or function begins execution, all of its local variables are automatically initialised, that is given an initial value.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
These default values can be found in the data types table earlier.
Implicitly-typed variables must be initialized


No Default Values

In C# there are no default values assigned to your variables.
C# does not assign any default values to local variables when they are declared.
Instead it requires you to always initialise the variables before they are used
A compilation error will occur if you try to read a value from a local variable before it has been initialised.


VB however assigns default values to all its local variables.
VB automatically initializes all variables to 0 or nothing.


When a procedure or function begins execution, all of its local variables are automatically initialised, that is given an initial value.
Explicitly initialising all your local variables makes your code more readable and is a good habit to get into.
Static variable however cannot be initialised, due to there fundamental purpose.
These default values can be found in the data types table


Numeric variables (integer, short, long, double, currency) are initialised to zero
Variable length strings are initialised to a zero-length (or empty) string
Fixed length strings are initialised with the character represented by the ASCII code 0, or Chr(0).
Variant variables default to Empty
Object variables default to Nothing


When a subroutine starts all the local variables are automatically initialised to their default values.


string firstName = "", lastName = ""; 



Dim sFirstName As String 
'the automatic initialisation will set its initial value to the empty string ""

It is not good programming practice to rely on these automatic initial values.
You should always try and explicitly specify the initial value.

Dim sFirstName As String 
   sFirstName = ""




© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext