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

Is C# LINQ fully reliable/supported?

Discussion in 'Scripting' started by ArachnidAnimal, Jan 13, 2016.

  1. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    I'm trying to use LINQ, but notice sometimes I get a bunch of compiler errors related to using LINQ. But then if I re-compile the code, the compiler errors no longer appear. They only appear about 5% of the time. It makes me nervous. (Sorry I don't have the actual compiler errors, because right now I cant reproduce the issue)
    I've read other threads where people say LINQ is reliable, others say it isn't.

    Here is the problematic code:
    Code (csharp):
    1.  
    2. public void TestLINQ(float ff)
    3.    {
    4.      string variableName = GetVariableName(() => ff);
    5.    }
    6.  
    7.    public static string GetVariableName<T>(System.Linq.Expressions.Expression<Func<T> > expr)
    8.    {
    9.      string varName = ((System.Linq.Expressions.MemberExpression) expr.Body).Member.Name;
    10.  
    11.      return (varName);
    12.    }
    13.  
    14.  
    The code is supposed to return the variable name. When the code compiles, it works at runtime.
    I know little about LINQ. I'm trying to use it for the process of serializing data for save/load games.
    Does anyone else have intermittent issues with LINQ?
     
    Last edited: Jan 13, 2016
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Don't use the default LINQ if you plan to publish an iOS build. Methods like OrderBy and alike may produce AOT/JIT errors.
    Use UniLinq instead:
    https://github.com/RyotaMurohoshi/UniLinq

    I've been extensively using it in my current project, works great.
     
    ArachnidAnimal likes this.
  3. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    Ok, just to be safe I found a different method, avoiding LINQ.
    I don't fully understand LINQ and don't have a warm feeling about it.

    i was able to replace the code by using
    System.Runtime.InteropServices
     
    Last edited: Jan 13, 2016
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    System.Runtime.InteropServices is usually used for totally different kind of operations. I honestly would not use it for replacing LINQ. And don't be afraid, LINQ is typically one of the best dev code extensions out there and is heavily used in C# projects. You should be good to go with it as it will sooner or later simplify your live a lot.

    Just as an example let's assume you have numerous entries of some kind of data (as a model) collected in a list "myInputList" and you just want to work with a sub selection of that list where the SelectionType property is "TakeMe".

    Code (CSharp):
    1. // LINQ way
    2. var myOutputList = myInputList.Where(entry => entry.SelectionType == "TakeMe");
    3.  
    4. // old way
    5. var myOutputList = new List<MyModel>();
    6. foreach(var entry in myInputList)
    7. {
    8.     if (entry.SelectionType == "TakeMe")
    9.     {
    10.         myOutputList.Add(entry);
    11.     }
    12. }
    So instead of writing 8 lines of code you have just 1.

    No, I'm not paid by Microsoft, I just try to encourage easily readable and writable code. ;)
     
    Last edited: Jan 13, 2016
    asid_ch and ArachnidAnimal like this.
  5. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,727
    It looks like this is worth learning.
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Unity's running on an old version of Mono, which among other bug has some nasty issues where collections generate garbage.

    The most prominent example is a few bytes generated by any foreach-loop, but I'm pretty sure that many of the linq calls generates some garbage. This doesn't mean "Don't use LINQ", it just means "avoid it in loops that run a lot".
     
    Fajlworks and ArachnidAnimal like this.
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    LINQ can make some tasks with lists and collections so much easier with much more concise code, but as mentioned is probably best avoided in places where performance is paramount.