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
随机推荐
- 7款超酷HTML5 3D动画精选应用及源码
对以前来讲,3D动画拿到网页上展示是一件非常奢侈的事情,第一是浏览器不够先进,第二是大部分只能用flash实现伪3D.HTML5的出现,让实现网页3D动画变得非常简单,当然前提是你不要再使用像IE67 ...
- 8款强大的CSS3/HTML5动画及应用源码
1.CSS3 jQuery UI控制滑杆插件 今天我们要来分享一款基于CSS3和jQuery UI的控制滑杆插件,这款控制滑杆是在HTML5滑杆元素的基础上进行自定义CSS3渲染,所以外观更加漂亮.另 ...
- mount.nfs: access denied by server while mounting localhost:/home/xuwq/minilinux/system
在执行命令如下: mount -t nfs localhost:/home/xuwq/minilinux/system /mnt 出现的错误: mount.nfs: access denied by ...
- SQL中的模糊查询
写个标题先.先来一篇大神的文章:http://www.cnblogs.com/GT_Andy/archive/2009/12/25/1921914.html 练习代码如下: 1.百分号:% 表示任 ...
- Configure Log Shipping
准备工作 两台装有的Windows Server 2012R2以及SQL Server 2012的服务器 下载评估版 Windows Server 2012 R2 下载 Microsoft SQL S ...
- opencv学习笔记(04)——ROI
ROI的用法:1.直接相加:2.掩码法 #include <opencv2\highgui\highgui.hpp> #include <opencv2\imgproc\imgpro ...
- 数据分析≠Hadoop+NoSQL,不妨先看完善现有技术的10条捷径(分享)
Hadoop让大数据分析走向了大众化,然而它的部署仍需耗费大量的人力和物力.在直奔Hadoop之前,是否已经将现有技术推向极限?这里总结了对Hadoop投资前可以尝试的10个替代方案, ...
- ORACLE 11G 配置DG 报ORA-10458、ORA-01152、ORA-01110
操作系统: Oracle Linux Server release 5.7 数据库版本: Oracle Database 11g Enterprise Edition Release 11.2.0.3 ...
- 【转载】MySQL 日志 undo | redo
本文是介绍MySQL数据库InnoDB存储引擎重做日志漫游 00 – Undo LogUndo Log 是为了实现事务的原子性,在MySQL数据库InnoDB存储引擎中,还用Undo Log来实现多版 ...
- MySQL 多实例数据库还原脚本-备份集与端口对应
版本:5.5.14 OS: ConetOS 6.3 1.创建recover.sh [root@yoon export]# vi recover.sh #!/bin/bash bakdir=/exp ...