Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why Public variables cannot be declared as VarType where as dynamic is allowed?

When i declared a variable as Var type as like the following:

C#
public Var xVariable; // It says Type or namespace could not be found

where as it allows the the declaration as
C#
public dynamic xVariable;
Posted
Comments
BillWoodruff 3-May-15 2:14am    
You might find it useful to think of "var" as compiler "rule:" "when you see the term 'var in a Method on the left-hand side of the assignment operator, then figure out the Type of the "stuff" on the right-hand side of the assignment operator, and use that Type for the variable name."

Even if you got the case right - Var is not the same as var - you couldn't do that because the type of a var variable is defined by the context in which it is used at compile time, and can never change. and the specification says that var can only be used with variables declared within methods, so even if you did provide context:
C#
public var s = "hello";
public var t = GetListOfUnits();
would still not be allowed.
var is an implicit typing, which was intended to be used with Linq, where the type of a return value may not correspond to any declared type (or may be a collection of an undeclared type) - it isn't meant to be used instead of a variable type like Dim in VB is.

dynamic variables are different: the type of these is evaluated at run time, it is specifically not checked at compile time.
 
Share this answer
 
Comments
BillWoodruff 3-May-15 2:10am    
+5
It might look like it at first glance, but actually var and dynamic have almost nothing in common.

The actual type of variables declared as dynamic is evaluated at runtime and can be changed during runtime. So the compiler doesn't care much about what you're doing with it and allows almost anything. If you're doing something wrong with it, it will bite you at runtime.

var on the other hand is just a syntax-shorthand *. When declaring a variable using var, the compiler has to have a way to determine the actual type for the variable, which means you have to assign a value to it "on the same line" so the compiler can infer the type for the variable from the type of the value you're assigning to it. When trying to declare a variable with var like you did in your question, there is no way for the compiler to infer a concrete type for it and it would be the same if you tried to do this for a local variable in a method. So, as a result of the compiler determining the actual type for the variable, the compiler will enforce syntactical correctness for the use of the variable and the type also can't be changed during runtime (in contrast to dynamic). As long as you're not dealing with "anonymous types" you could replace every var-keyword in your source code by the concrete type name and it wouldn't change a thing.

* : With the exception of "anonymous types" where it's neccessary because there won't be a static type name for the compiler generated "anonymous" type. But this changes nothing about what's being said above.
 
Share this answer
 
Comments
BillWoodruff 3-May-15 2:10am    
+5
Sascha Lefèvre 3-May-15 5:58am    
Thank you, Bill.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900