1

I am using Unity 5.5.0f3 which uses .Net 2.0

I want to cast a list like this but cannot in this version of .Net

Is there an 'old school' or 'long hand' way of doing it?

Please tell me that I don't have to create my own extension method involving loops......

Community
  • 1
  • 1

1 Answers1

1

You can! If you mean Enumerable.Cast when you say cast then it is available in .NET 3.5 and Unity supports that. With the example code from MS doc, I was able to compile it.

You just need to include using System.Linq in order to use it. When in doubt, ,you can search for other Unity Mono Compatibly functions here.

using System.Linq
...
void Start()
{
    System.Collections.ArrayList fruits = new System.Collections.ArrayList();
    fruits.Add("apple");
    fruits.Add("mango");

    IEnumerable<string> query =
        fruits.Cast<string>().Select(fruit => fruit);

    foreach (string fruit in query)
    {
        Debug.Log(fruit);
    }
}
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Turns out that I was wrong about my unity version's .Net version. I also wasn't including linq. Thanks. I admit it was a pretty dumb question in the end. – Lorry Laurence mcLarry Jan 31 '17 at 05:24
  • You are not entirely wrong. Unity supports .NET 2 and supports **some** functions from .NET Net 3. Some classes and functions from .NET 3 are not supported. – Programmer Jan 31 '17 at 05:26