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 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 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!
Implicitly Typed Local Variables的更多相关文章
- Effective Java 45 Minimize the scope of local variables
Principle The most powerful technique for minimizing the scope of a local variable is to declare it ...
- Results from queries can be retrieved into local variables
w将查询结果赋值给本地变量. http://dev.mysql.com/doc/refman/5.7/en/stored-program-variables.html Results from que ...
- 【java】A local class access to local variables
内部类参考 A local class has access to local variables. However, a local class can only access local vari ...
- 栈帧的内部结构--局部变量表(Local Variables)
每个栈帧中包含: 局部变量表(Local Variables) 操作数栈(Opreand Stack) 或表达式栈 动态链接 (Dynamic Linking) (或指向运行时常量的方法引用) 动态返 ...
- This inspection warns about local variables referenced before assignment.
关于 local variable 'has' referenced before assignment 问题 今天在django开发时,访问页面总是出现错误提示“local variable 'ha ...
- QIBO CMS /inc/common.inc.php Local Variables Overriding Vul In $_FILES
目录 . 漏洞描述 . 漏洞触发条件 . 漏洞影响范围 . 漏洞代码分析 . 防御方法 . 攻防思考 1. 漏洞描述 齐博在/inc/common.inc.php使用$$_key=$value.ext ...
- MyBatis java and MySql local variables
<insert id="create" parameterType="models.entities.CategoryEntity"> set @c ...
- NDK: unable to watch local variables after using GCC4.8
the problem definitly apears after changing toolchain from gcc 4.6 to gcc 4.8. here's a solution wit ...
- 为何 Delphi的 Local Variables 突然没有值显示了
可能是上次编译后 code未再修改过. 试试 随便 输入一个空格,然后F9
随机推荐
- 完美解决移动Web小于12px文字居中的问题
前几天的一篇博文:移动Web单行文字垂直居中的问题,提到了移动web里小于12px的文字居中异常的问题,最后还是改为12px才近乎解决了问题.但是有时候或许并不是那么乐观,你并不能将原本定为10px的 ...
- SSH连接 NAT型 VirtualBox + LINUX
1.首先登录到虚拟机中的Linux系统,查看一下使用NAT网卡的IP地址. 2.关闭虚拟机. 3.依次点击 "设置 -> 网络 -> (网络地址转换)端口转发",其中需 ...
- Linux C 程序 信号及信号的处理(19)
信号及信号的处理 1.Linux信号的介绍 信号是一种软件中断.Linux系统中根据POSIX标准扩展的信号机制. 1.信号来源 1.硬件方式 1.当用户按下某个键, ...
- 【个人】IIS Express 配置
<!-- 查看URL访问控制列表: netsh http show urlacl 添加URL访问控制: netsh http add urlacl url=http://myhostname:8 ...
- WIN2003跳出res://C:WINDOWSsystem32mys.dll/mys.hta解决方法
出现这个问题的时候 @echo off 请将以下语句复制到记事本中,另存为后缀为.cmd的文件,并运行.当然在命令行下一句句运行也没问题. echo 正在修复,这个过程可能需要几分钟,请稍候…… ru ...
- 我的总结SVN的使用
SVN平时在用时,一般要做到早上一次更新,中间每做出一个模块就要提交,就是一次更新,多次提交, SVN在修改公共类,配置文件时一定要注意,如果修改的是配置文件中的东西,这样修改完之后一定要提交, 如果 ...
- 多线程更新已排序的Datagridview数据,造成数据错位
多线程更新已排序的Datagridview数据,触发Datagridview的auto-sort时间,数据重新排序,造成后面更新数据的更新错误. 解决方法: 方法一.设置Datagridview的表头 ...
- Dynamic - ExpandoObject学习心得
1. 今天下午在做开发过程中,遇到了一个问题,要往Xml文件中添加新的节点,做个xml开发的都知道该怎么做,这不是什么难事,我卡卡卡卡把这个问题解决了,但是新问题又来了,要对xml中对应的节点数据添 ...
- TCP连接,传输数据时的粘包问题讨论
第一个需要讨论的大概就是粘包问题了.因为这个是TCP的个性问题,UDP通信时不存在这个问题的.首先看一下什么叫粘包: 客户端采取与服务器的长连接方式建立通信(Open-Write/Read-Write ...
- rtx信息泄漏利结合弱口令导致被批量社工思路
腾讯通RTX(Real Time eXchange)是腾讯公司推出的企业级实时通信平台. rtx server 存在暴露用户信息的漏洞,通过web访问 http://RtxServerIp:8012/ ...