Implicitly typed local variables
It happens time and time again: I'll be at a game jam, mentoring students, or just showing a friend some unity C # code, and I'll type something like this:
var controller = GetComponent<CharacterController>(); |
They'll go, "whoa wait, isn' tVaRA JavaScript thing ?"
Well the answer is Yes... And no, the keywordVaRExists in both unity's C # and JavaScript versions, but they actually mean different things. in C #, it means I'm defining an implicitly typed local variable.
Implicitly typed local variables
When you useVaROn a local variable in C #, it means that the compiler will attempt to infer the variable's type based on context. This differs fromDynamic VariableIn that it will be fast and strictly-typed as if you 'd defined the type yourself.
To explain better, consider the following two lines of code:
var controller = GetComponent<CharacterController>(); CharacterController controller = GetComponent<CharacterController>(); |
Despite which of these two lines of code you use, unity will compile themIdentically.One is not faster or slower than the other. Personally, I really likeVaRFor this purpose, because I don't really like having to type long-winded names like "charactercontroller" twice to define a single variable.
If you're a fan of monodevelop or Visual Studio's Code hinting, you'll be happy to know that usingVaRKeyword will not cause it trouble, it will still give you correct code hints on the fly as you type.
Thoughts on usage
Some coders refuse to use implicit typing completely, as it can sometimes be unclear to other readers of the Code what type a variable actually is. so I'll suggest a simple guideline I use: only use implicit typing when the type of the variable is obvious in context. for example:
//This is okay, it is obvious here that the type is "Movement Controller" var moveController = GetComponent<MovementController>(); //Less okay, not necessarily obvious what this function is returning var properties = moveController.GetProperties(); //Unless it is obvious, this is probably a better approach MoveProperties properties = moveController.GetProperties(); //This I am actually okay with, "targets" is defined right here in plain sight GameObject[] targets = GameObject.FindGameObjectsWithTag( "Enemy" ); var firstTarget = targets[0]; |
You don't have to follow this rule, but I think it is fair. at least you'll now get to know the joy of telling somebody "Actually, this is a feature of C #!" When they incredulously ask you why you are using a javascript keyword.
Also Finally, keep in mind thatMember variables cannot be implicitly typed, So for example:
using UnityEngine; using System.Collections.Generic; public class Player : MonoBehaviour { //This is a member variable, so you can‘t use "var" here List<Transform> targets = new List<Transform>(); void Update() { //You can only use them for local vars, like this! var firstTarget = targets[0]; //Or even as iterator variables foreach ( var target in targets) { } } } |
Hope you enjoyed this article. If you have any questions or feedback, leave a comment below!
Implicitly typed local variables