zoukankan      html  css  js  c++  java
  • Implicitly Typed Local Variables

    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’t var a JavaScript thing?”

    Well the answer is yes… and no, the keyword var exists in both Unity’s C# and JavaScript languages, but they actually mean different things. In C#, it means I’m defining an implicitly typed local variable.

    Implicitly Typed Local Variables

    When you use var on a local variable in C#, it means that the compiler will attempt to infer the variable’s type based on context. This differs from a dynamic variable in 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 them identically. One is not faster or slower than the other. Personally, I really like var for 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 using the varkeyword 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 that member 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!

  • 相关阅读:
    ssh免密钥登陆的两种方式
    python 项目实战之装饰器
    python 项目实战之随机杀死程序
    python paramiko外部传参和内部调用命令的方法
    linux screen 工具
    shell删除三天前或者三天内的文件
    CentOS7.3安装Go运行和开发环境
    4.Linq to Xml
    30.第一个Linq 数据库查询
    html 图标库
  • 原文地址:https://www.cnblogs.com/chongxin/p/4025118.html
Copyright © 2011-2022 走看看