0

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:

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

where as it allows the the declaration as

public dynamic xVariable;
  • This has nothing to do with "public". You have attributed the compiler error to an arbitrary element of the program. – usr May 02 '15 at 12:36
  • How is the compiler supposed to know what type you want? `Var` is not a type. Sounds like you need to read up on the difference between `var` and `dynamic`. – Ant P May 02 '15 at 12:37
  • `var` is used in method scope. – kennyzx May 02 '15 at 12:38

1 Answers1

1

You can not use VAR as a return type as it is a shorthand type notation used where the type can be determined at compile time. Although that might be known within your class, it can change within your inplementation without a consumer of that method knowing.

Returning a var would also be a problem were you to assign it to another variable, effectively saying var i = var.

Dynamic is a special type whereby the type is almost not known and coded around.

kidshaw
  • 3,423
  • 2
  • 16
  • 28