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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Lambda expression for variables

Discussion in 'Scripting' started by Limprofit, Apr 13, 2021.

  1. Limprofit

    Limprofit

    Joined:
    Jan 14, 2019
    Posts:
    2
    Just had a quick question about lambda expressions and their performance. I'm trying to have a class method like so:
    Code (CSharp):
    1. public bool cond1, cond2;
    2.  
    3. public bool Compare() {
    4.     return cond1 || cond2;
    5. }
    But then I thought, can't I use a lambda for this?

    Code (CSharp):
    1. public bool cond1, cond2;
    2. public bool compare => cond1 || cond2;
    My question is, is one method better than the other performance-wise? I'm not quite sure when a lambda is called if assigned to a variable, is it just when the variable is accessed?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Performance investigations should start with Window -> Analysis -> Profiler

    Alternatively you can try both methods and do your own benchmarking.

    Remember, running in editor is not an indication of performance on target platform. Every target will have different performance.
     
    Bunny83 and Limprofit like this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    This is just syntactic sugar. Both snippets of code are going to compile down to the same thing more or less.

    `compare` is not a variable - it's a property with only a getter, which is ultimately just a method like in the first example.
     
  4. Limprofit

    Limprofit

    Joined:
    Jan 14, 2019
    Posts:
    2
    Ah so it's essentially a pointer to the getter, that makes sense. Thanks, Kurt and Praetor.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,539
    Well, no pointers are involved here, so your wording is a bit confusing. Just to be clear, this line

    Code (CSharp):
    1. public bool compare => cond1 || cond2;
    is just syntactic sugar for an property like this

    Code (CSharp):
    1. public bool compare
    2. {
    3.     get
    4.     {
    5.         return cond1 || cond2;
    6.     }
    7. }
    C# / .Net properties are literally just a pair of methods. The compiler will actually generate a method called "get_compare". However those methods are only accessible and visible to the compiler. You can only directly interact with the property itself. Though when you have a line like

    bool tmp = compare;


    the compiler actually generates this code under the hood

    bool tmp = get_compare();


    If you want to know the difference and how the actual IL code looks like that is generated, I would recommend to use ILSpy. Try both variants and open your compiled Assembly-CSharp.dll in ILSpy. You can find your script dll under:

    YourProjectFolder/Library/ScriptAssemblies/


    ILSpy can decompile your code back to C# which usually isn't that helpful. However you can switch to viewing IL code. The latest version even has an "IL with C#" mode where it adds the C# code as comments in between and highlights that part that is actually implemented at a certain point.
     
    Last edited: May 5, 2021
  6. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    In C#, you can use these lambda expressions for any one-line long methods:
    Code (CSharp):
    1. public class Example {
    2.   private int someInt;
    3.  
    4.   //Constructor
    5.   public Example() => someInt = 1;
    6.  
    7.   //Method
    8.   void DoSomething() => someInt = 2;
    9.  
    10.   //Read-only Property.
    11.   public int SomeInt => someInt;
    12. }
    But as stated, this is purely syntactic sugar and doesn't change anything about how the code is compiled or executed.
     
    Limprofit and Bunny83 like this.