Data Type - var

Added at .NET 3.0
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 (or most appropriate) data type.

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

instead of

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

Using var with Value data types is rather trivial, but it can save a lot of typing for Reference data types
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 typed 

Data Type Suffixes

L or l for long
D or d for double
F or f for float
M or m for decimal
U or u for unsigned integer
UL or ul for unsigned long

var myLong = 4294967296L;  
var myDecimal = 300.5m;
var myFloat = 3.145f;

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.

VB.Net - Variant

Using the Variant data type is generally a waste of 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" };

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