C# series 1.8 : Var Keyword - ponda

Breaking

7/08/2017

C# series 1.8 : Var Keyword

The var Keyword

a variable can be explicitly declared with its type before it is used. Alternatively, C# provides a handy function to enable the compiler to determine the type of the variable automatically based on the expression it is assigned to.

The var keyword is used for those scenarios :
Var num = 15;

The code above makes the compiler determine the type of the variable. since the value assigned to the variable is an integer, the variable will be declared as an integer automatically.

Variables declared using the var keyword are called implicitly typed variables. Implicitly typed variables must be initialized with a value. For example, the following program will cause an error.

var num;
num = 42;

Although it is easy and convenient to declare variables using the var keyword, overuse can harm the readability of your code. Best practice is to explicitly declare variables.

Next : C# series 1.9 : Constant

No comments:

Post a Comment