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

Cannot implicitly convert type - List is listed as IEnumerable?.

Discussion in 'Scripting' started by Quazaka, Aug 15, 2016.

  1. Quazaka

    Quazaka

    Joined:
    Jul 3, 2016
    Posts:
    15
    Hi there.
    I am trying to execute the following line of code:
    Code (CSharp):
    1.         List<GameObject> TempList = jumpEnemyList.Except(IHaveBeenHit); // Store enemies in jump range that have not been hit.
    2.  

    But it return the following error as it says my two collections are not the same:
    Code (CSharp):
    1. Severity    Code    Description    Project    File    Line    Suppression State
    2. Error    CS0266    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<UnityEngine.GameObject>' to 'System.Collections.Generic.List<UnityEngine.GameObject>'. An explicit conversion exists (are you missing a cast?)    CraftyTower.CSharp  
    3.  
    And this is where i am lost, both my List are created exactly the same way, as seen below. How come IHaveBeenHit suddenly be treated as an IEnumerable and how do i come about this problem?
    I feel like casting is weird as both of them are lists.


    Code (CSharp):
    1.     private List<GameObject> IHaveBeenHit = new List<GameObject>(); //List to store enemies already hit by this chain jump
    2.     private List<GameObject> jumpEnemyList = new List<GameObject>(); //List to store enemies that can be jumped to
    Thank you.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    What does jumpEnemyList.Except(IHaveBeenHit) return? Is it a List<GameObject>?
     
  3. Quazaka

    Quazaka

    Joined:
    Jul 3, 2016
    Posts:
    15
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Oh, ic. You're trying to use linq. So you need the ToList() after it.

    jumpEnemyList.Except(IHaveBeenHit).ToList();
     
  5. Quazaka

    Quazaka

    Joined:
    Jul 3, 2016
    Posts:
    15
    Okay, I also tried it and it did work, but I am just puzzles as to why one of the lists is considered an IEnumerable.
     
  6. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    When doing linq operations it treats almost all collection types as a IEnumerable, which provides a ToList() and a ToArray() method in case you need to cast it back to a list.

    IEnumerable is a interface that most collection types implement, that allows all collection types to be treated in a similar manner.
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Yep. Which is also while you'll see Linq cast into var most of the time so you don't have to worry about that casting. As you'll still be able to loop through it if needed.
     
    Quazaka likes this.
  8. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,738
    yeah once you are familiar with most of the types, using `var` can be rather useful. Helps when working in linq, also can be way the hell less redundant. If you are fairly new to programming in general i would recommend to use the proper typeNames so you more quickly learn them, but after that the shorthand var is really useful.
    Code (CSharp):
    1. List<float> mylist = new List<float>();
    2. vs
    3. var myList = new List<float>();
     
    Last edited: Aug 15, 2016
    Quazaka likes this.
  9. Quazaka

    Quazaka

    Joined:
    Jul 3, 2016
    Posts:
    15
    Thanks for the help guys :) - Really helps when you understand how the stuff works behind the scene.