Declaring - var

The var keyword is short for variable and does not mean variant.
The var keyword does not indicate that the variable is loosely typed, or late-bound.
The compiler automatically replaces "var" with the correct data type.

var student = new Company.Namespace.People.Person(); 

instead of

Company.Namespace.People.Person student = new Company.Namespace.People.Person(); 

Variables that are declared at method scope can have an implicit type "var".
The compiler automatically replaces "var" with the correct data type.
This cannot be used in parameter lists
This cannot be used in a class field
This implicitly typed local variable is strongly typed the only difference being that the compiler determines the type
Implicitly typed variable declarations must include an initializer (=).

var myvar = 10;    // implicitly typed 

instead of

int myvar = 10;    // explicitly types 

The var keyword does not mean variant and does not indicate that the variable is loosely typed, or late-bound.
The var keyword means that the compiler determines and assigns the most appropriate data type.
Using var with Value data types is rather trivial, but it can save a lot of typing for Reference data types

var student = new Company.Namespace.People.Person(); 

instead of

Company.Namespace.People.Person student = new Company.Namespace.People.Person(); 

Implicitly Typed Arrays

You can create an implicitly typed array in which the type of the array instance is inferred from the elements specified in the array initializer.
The rules for an implicitly typed variable also apply to implicitly typed arrays

var myarray = new[] {1,2,3,4} 
var myarray = new[] {"one","two","three"}

Object not Variant

There is no Variant data type in C# or VB.NET. The closest data type in .NET is Object.


Variant - VB.Net

Using the Variant data type is generally a waste to memory since Variants require more memory than other data types.
The following two lines of code are equivalent.

Dim sFirstName 
Dim sFirstName As Variant

Using a Variant data type may be useful sometimes but generally this should be avoided.
Because you know that FirstName will always contain a string you should declare this variable as:

Dim sFirstName As String 

An error occurs when the variant variables containing Curency, Decimal and Double values exceed their respective ranges.
The value Empty denotes a variant variable that has not been assigned to an initial value. A Variant containing Empty is 0 if it is used in a numerical context and a zero length string ("") if used in a string context.


The value Null denotes a variant variable that intentionally contains no data. The Value Bull denotes that an erro condition has occurred. Normal error handling does not occur giving you the chance to take alternative action in code


The variant data type can also contain the special values "Error", "Null", "Nothing" and "Empty". There are also the corresponding functions IsNull, IsError and IsEmpty


An object variable has not been set if it is assigned to "Nothing".


A variant can represent Empty, Error, Nothing and Null.
You can use the TypeName() function to return the type of a variable


The variant data type can be used for all data types except fixed length strings and user defined types.




Use implicit typing for local variables when the type of the variable is obvious from the right side of the assignment, or when the precise type is not important.
Don't use var when the type is not apparent from the right side of the assignment. Don't assume the type is clear from a method name. A variable type is considered clear if it's a new operator or an explicit cast.
Don't rely on the variable name to specify the type of the variable. It might not be correct. In the following example, the variable name inputInt is misleading. It's a string.
Avoid the use of var in place of dynamic. Use dynamic when you want run-time type inference.
Use implicit typing to determine the type of the loop variable in for loops.
Don't use implicit typing to determine the type of the loop variable in foreach loops.



If you use explicit instantiation, you can use var.
var vowels2 = new string[] { "a", "e", "i", "o", "u" };





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