Hello…
I’ve been usind C# for quite some time and i am usually defining my variables with their type.
Anyway i have recently watched some tutorials and the person, that explains them is using “var” to declare variables. Why is that? Is it good for some reason or it’s just a way to define a variable that you don’t know the type of, at the time of declaration? If so, does Unity re-declare it, when it knows the type? Coud that bring some errors…
For example this piece of code:
var vertColors = new Color[mesh.vertexCount];
for (var i = 0; i < mesh.vertexCount; i++)
{
vertColors *= color;*
-
}*
-
mesh.colors = vertColors;*
-
mesh.RecalculateNormals();*
Why using var for declaring “vertColors”? And why isn’t Unity expecting to have “var[]” for declaring an array?
The first line can be also written like this:
Color[] vertColors = new Color[mesh.vertexCount];
but here Unity expects me to write [], after the type of the variable. Why it doesn’t give error, on the declaration (with var) above??
The tutor is also isung it to declare the “i” for the loop… I don’t get that at all…
So it seems that i can declare every type with var, instead of the type. Is that more expensive, than declaring the exact type?
As a whole, why woud i use var…
Thanks