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!

转载于:https://www.cnblogs.com/chongxin/p/4025118.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值