Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Declaring a variable as var in c#

Discussion in 'Scripting' started by vikingjonsson, Mar 2, 2016.

  1. vikingjonsson

    vikingjonsson

    Joined:
    Feb 26, 2016
    Posts:
    6
    Hello,

    I was looking at a tutorial at lynda.com. In this tutorial I'm supposed to get the camera component from the main camera game object. The script is made in C#.
    I got a bit curious when they declare a variable as var. The write:
    (Don't mind the row numbers, didn't know how to get rid of them)

    Code (CSharp):
    1. var camera = GetComponent<Camera>();
    I usually declare a private variable of the same typ as the component I want to get.
    In this case I would do like this:

    Code (CSharp):
    1. private Camera camera;
    2. camera = GetComponent<Camera>();
    I thought thet C# was pretty hard on the variable types (just like Java) and this looks more like a JavaScript/UnityScript type of variable.

    I tried searching for an answer but I only found forum posts about the difference in C# and UnityScript and lists over the type of variables that exists in C# and "var" was not mentioned.

    So now I'm curious, why is this possible and is there any benefit or down side to it?

    Regards
    Viking
     
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    "var" is just a way to tell the compiler to figure out the variable type by itself, it isn't a type in itself. The compiler changes it to "Camera" for you.

    I don't use it either, but I'm pretty sure most languages have a way of doing this type of thing anyway.
     
    vikingjonsson likes this.
  3. vikingjonsson

    vikingjonsson

    Joined:
    Feb 26, 2016
    Posts:
    6
    Thank You Todd! I guess the type of variable needs to be obvious to the compiler to be able to use var and in most cases it might be now when i think about it.. In my case it should be cause of the "GetComponent<Camera>(); I get the point but it feels a bit scary to do it like that :)
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    Yep, you can only use 'var' in C# if there is an unambiguous return type in whatever you initially assign to it.
     
  5. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    They added it to the language primarily to support LINQ, which can return types that the compiler can "declare" but puny human programmers cannot. Personally I think it's kind of sloppy to use it anywhere else.
     
    Todd-Wasson likes this.
  6. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,077
    Yep, I steer clear of var. Don't think I have a single instance anywhere in my current game.
     
  7. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,735
    It can make the code a lot clearer, less verbose, and more durable to changes, especially while a piece of code is under active development. I use it a lot with dictionaries, for one. For example, this:
    Code (csharp):
    1. Dictionary<string, ClassOne> someDict = new Dictionary<string, ClassOne>();
    2. foreach (KeyValuePair<string, ClassOne> dictKVP in someDict) {
    becomes this:
    Code (csharp):
    1. var someDict = new Dictionary<string, ClassOne>();
    2. foreach (var dictKVP in someDict) {
    If I later decide that I want the value to be ClassTwo instead of ClassOne, I only have to change that in one place in that code, not three.
     
    ericbegue and Todd-Wasson like this.
  8. vikingjonsson

    vikingjonsson

    Joined:
    Feb 26, 2016
    Posts:
    6
    StarManta you have a good point there. It can get a bit tire soem to make a change and realise that you have to fix it in other places as well.

    I think I will try it out and see if it works out for me. Thanks everyone for the fast answers and support!
     
  9. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I use var all the time!

    The comparaison with soft-typed language is not correct. In C#, var is strong-typed! The compiler infer the type from the context yet the type is immutable as opposed to languages as javascript where a variable can change its type at anytime. The compiler won't compile if the type is ambiguous. That's why you can't do things like:

    Code (CSharp):
    1. var a = null;
    But the following is perfectly fine :

    Code (CSharp):
    1. var a = (string)null;
    The compiler will complaint if you do:

    Code (CSharp):
    1. var x = 3;
    2. x = "Hello World!";
    And be honest, what do you prefer to read.
    That:
    Code (CSharp):
    1. Dictionary<string, List<float>> myDictionary = new Dictionary<string, List<float>>();
    or this:

    Code (CSharp):
    1. var myDictionary = new Dictionary<string, List<float>>();