Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize var?

Can I initialize var with null or some empty value?

like image 430
maztt Avatar asked May 25 '10 12:05

maztt


People also ask

How do you initialize a variable?

The way of initializing a variable is very similar to the use of PARAMETER attribute. More precisely, do the following to initial a variable with the value of an expression: add an equal sign (=) to the right of a variable name. to the right of the equal sign, write an expression.

How do I initialize a variable in Java?

The syntax for an initializer is the type, followed by the variable name, followed by an equal sign, followed by an expression. That expression can be anything, provided it has the same type as the variable. In this case, the expression is 10, which is an int literal.

How do you initialize a variable in C++?

For example, to declare a variable of type int called x and initialize it to a value of zero from the same moment it is declared, we can write: int x = 0; A second method, known as constructor initialization (introduced by the C++ language), encloses the initial value between parentheses ( () ):

How do you initialize a variable in C#?

Local Variables Declarations specify the type followed by the name, and optionally the initialization. Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type. In the above code, the variable a is declared as a string and is initialized to "Hello World".


2 Answers

C# is a strictly/strongly typed language. var was introduced for compile-time type-binding for anonymous types yet you can use var for primitive and custom types that are already known at design time. At runtime there's nothing like var, it is replaced by an actual type that is either a reference type or value type.

When you say,

var x = null;  

the compiler cannot resolve this because there's no type bound to null. You can make it like this.

string y = null; var x = y; 

This will work because now x can know its type at compile time that is string in this case.

like image 64
this. __curious_geek Avatar answered Oct 05 '22 11:10

this. __curious_geek


Well, as others have stated, ambiguity in type is the issue. So the answer is no, C# doesn't let that happen because it's a strongly typed language, and it deals only with compile time known types. The compiler could have been designed to infer it as of type object, but the designers chose to avoid the extra complexity (in C# null has no type).

One alternative is

var foo = new { }; //anonymous type 

Again note that you're initializing to a compile time known type, and at the end its not null, but anonymous object. It's only a few lines shorter than new object(). You can only reassign the anonymous type to foo in this one case, which may or may not be desirable.

Initializing to null with type not being known is out of question.

Unless you're using dynamic.

dynamic foo = null; //or var foo = (dynamic)null; //overkill 

Of course it is pretty useless, unless you want to reassign values to foo variable. You lose intellisense support as well in Visual Studio.

Lastly, as others have answered, you can have a specific type declared by casting;

var foo = (T)null; 

So your options are:

//initializes to non-null; I like it; cant be reassigned a value of any type var foo = new { };   //initializes to non-null; can be reassigned a value of any type var foo = new object();  //initializes to null; dangerous and finds least use; can be reassigned a value of any type dynamic foo = null; var foo = (dynamic)null;  //initializes to null; more conventional; can be reassigned a value of any type object foo = null;  //initializes to null; cannot be reassigned a value of any type var foo = (T)null; 
like image 25
nawfal Avatar answered Oct 05 '22 12:10

nawfal