- Home /
[C#] "var" problem
Every time i use "var" i get this strange error:
Assets/NewBehaviourScript.cs(49,37): error CS1525: Unexpected symbol
:', > expecting
)',,',
;',[', or
='
Example of code I was using:
var texture : Texture;
Answer by nsxdavid · Feb 24, 2013 at 05:38 PM
var texture : Texture is not C#. That's JavaScript.
You want:
Texture texture; // This is C#
Answer by vladibo · Feb 24, 2013 at 06:31 PM
In C# you never use
var texture;
only
var texture = something...
and only within methods, if this is a class attribute you have to specify type as:
public Texture texture;
Answer by kuhfleisched · Feb 24, 2013 at 05:59 PM
if you use c# you dont use var for initializing a variable
here an example to how to do it
public float movingSpeed = 100;
hope it could help
var does exist in C#. It is used when you want to let the compiler make the decision on the type.
string str = "A string";
can be done var str = "A string";
or if you want to create an object at runtime:
var noType = new {name ="Steve", age = 10 };
Yes, and to add a bit more detail: in C# it's called inference typing. It infers the type from what follows. Which is different than var in JavaScript which is used to defined all variables.
Your answer
Follow this Question
Related Questions
Is OverlapCircle broken? 0 Answers
Using a footstep C# script and keep getting an "IndexOutOfRangeException" 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it a bug? 1 Answer