Implicitly typed local variables

Source: Internet
Author: User
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 returningvar properties = moveController.GetProperties(); //Unless it is obvious, this is probably a better approachMoveProperties properties = moveController.GetProperties(); //This I am actually okay with, "targets" is defined right here in plain sightGameObject[] 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.