- Home /
Does it matter to declare variable type?
var property1;
var property2 : float;
var property3 = 0.0;
Is there any difference between the above? Is any faster or inherently better than the other?
Bump because I would also like to know the answer and if it applies in C# in the same way
Answer by Owen-Reynolds · Nov 18, 2013 at 05:02 PM
var property : float;
is the real one. If you use one of the other two, the compiler just adds the float
for you. So they are all the same.
For style, declaring the float explicitly often makes the program easier to read, and can avoid some errors. If you use the short-cut, and assign property1=0;
(which is a perfectly fine shortcut for 0.0,) the compiler might guess property1: int
. Then, if you add 0.1 later, it gets confused (rounds back down to 0. Ick.)
Something like: hitPoints: int; life: float;
is often helpful. Tells you up front there will be no fractional hitpoints, but whatever "life" is will have fractions.
Great answer! This is what I believed already but it's nice to have confirmed. :)
Your answer
Follow this Question
Related Questions
Public and Static Variables 2 Answers
Using a variable as a components for Vector3 1 Answer
variable not declared? 1 Answer