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
随机推荐
- AMQ学习笔记 - 15. 实践方案:基于ActiveMQ的统一日志服务
概述 以ActiveMQ + Log4j + Spring的技术组合,实现基于消息队列的统一日志服务. 参考:Spring+Log4j+ActiveMQ实现远程记录日志——实战+分析 与参考文章的比较 ...
- (转)Linux概念架构的理解
英文原文:Conceptual Architecture of the Linux Kernel 摘要 Linux kernel成功的两个原因:(1)架构设计支持大量的志愿开发者加入到开发过程中:(2 ...
- js模版引擎Mustache介绍
Mustache通常被称为JavaScript模板的基础.另一个流行的解决方案Hanldebars实际上就是基于Mustache构建而成的.这并不意味着Mustache是一个不好的模板解决方案. 下面 ...
- javaScript入门2--基本概念
语法: 1.区分大小写!! 2.标识符:既变量,函数名,属性名,参数. 1.第一个字符必须为字母,_,或者$ 2.其他字符可以是字母,_,&,或者数字 3.注释: 1.单行注释:// 2.多行 ...
- Jquery 学习二
一.事件编程 1.基本事件(以方法形式存在的) 基本语法: 原生Javascript代码中的事件绑定方式: DOM对象.事件 = 事件的处理程序 jQuery代码中的事件绑定方式: jQuery对 ...
- sql简易的MRP资源分析
写了个简易的MRP根据传进来的数据,进行上下级的判断,父表,子表 构思: 3张变量表,第一张用来存传进来的成品,这边对表做了循环就是成品是一样一样进去的,取成品表的第一行,将数据做父表和子表关联,取出 ...
- 模拟抛硬币(C语言实现)
实现代码: #include<stdio.h> #include<stdlib.h> int heads() { ; } int main(int argc, char *ar ...
- Sql 执行删除修改之前添加备份
backyw备份滴数据库名称,w20151124sendmaster 表名称 select * into backyw..w20151124sendmaster from Logistics.E ...
- 重拾C,一天一点点_4_随想
刚才顺便又把二分默写了一遍,还好,这次比较顺利.算法这一块,一直是自己一块痛处,有时感觉自己的脑瓜子怎么就这么笨,后一想觉得肯定是锈逗了,确实啊,这么长时间不思考的脑子能机灵到哪呢?早就意识到这个问题 ...
- sqlserver中distinct的用法(不重复的记录)
下面先来看看例子: table表 字段1 字段2 id name 1 a 2 b 3 c 4 ...