iPhone 1.6 Generics Support

Hi,

I’m trying to get a read on exactly which C# generic collections are supported in iPhone 1.6. The following code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GenericTest : MonoBehaviour {

	private Queue<int> intQueue;
	private List<int> intList;
	private LinkedList<int> intLinkedList;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Produces these messages:

Assets/Scripts/GenericTest.cs(7,17): error CS0308: The non-generic type `System.Collections.Queue' cannot be used with the type arguments (Filename: Assets/Scripts/GenericTest.cs Line: 7)
Assets/Scripts/GenericTest.cs(9,17): error CS0246: The type or namespace name `LinkedList`1' could not be found. Are you missing a using directive or an assembly reference? (Filename: Assets/Scripts/GenericTest.cs Line: 9)

The same code compiles cleanly in Unity Pro 2.6. Are Queue and LinkedList not supported in iPhone 1.6? I have specified API Compatibility Level to .Net2.1.

Thanks!
Mal

Can anyone shed any light on this? Someone from UT perhaps? Any info would be much appreciated!

Cheers,
Mal

To my knowledge there is no list on which work and which won’t
Generally though all that rely on JIT compilation will not work.

List<> and Dictionary<,> are known to work

I don’t believe Stack is supported. I ran into the same problem.

-bb

Good to know. Thanks for the info. Is there any way to know which might rely on JIT? I’ve looked in my C#3.0 in a Nutshell and the MSDN Systems.Collection.Generic docs and I don’t see it there, at least not in a form I can understand.

It’s rather an academic point, I suppose. What we really need is a list of what’s supported from our friends at UT! :slight_smile:

Thanks bb. Good to know.

Cheers,
Mal

you will normally find it out the hardway by the crashlog on the iphone unless it relies on modules that are JIT only and bombs already at compile time

Here is a Queue implementation using Generics that should work in Unity iPhone 1.6 and on the device. Granted it doesn’t have half the functionality of “System.Collections.Generic.Queue<>”, but the basic features are there, and it’s type-safe and will not incur the cost or risk of runtime casts or boxing operations.

	public class Queue<T>
	{
		private uint _count = 0;
		private Node<T> _head = null;
		private Node<T> _tail = null;
		private Node<T> _temp = null;

		public bool IsEmpty { get { return (_count == 0); } }

		public uint Count { get { return _count; } }

		public T Peek()
		{
			if (IsEmpty) throw new Exception("The queue is empty");

			return _head.Value;
		}

		public void Enqueue(T obj)
		{
			if (_count == 0)
			{
				_head = _tail = new Node<T>(obj, _head);
			}
			else
			{
				_tail.Next = new Node<T>(obj, _tail.Next);
				_tail = _tail.Next;
			}
			_count++;
		}

		public T Dequeue()
		{
			if (IsEmpty) throw new Exception("The queue is empty");
			_temp = _head;
			_head = _head.Next;
			_count--;
			return _temp.Value;
		}

		public void Clear()
		{
			_head = _tail = _temp = null;
			_count = 0;
		}

		public override string ToString()
		{
			string result = "(";
			_temp = _head;
			while (_temp != null)
			{
				result += _temp.Value.ToString()
					+ (_temp.Next != null ? ", " : "");
				_temp = _temp.Next;
			}
			result += ")";
			return result;
		}
	}

The Node contains the value you want to store, and a pointer to the next node in the chain. This could also be used if you wanted to implement your own Stack<> class. You’d just have Next point to the “previous” element placed in the Stack.

	class Node<T>
	{
		public Node<T> Next;
		public T Value;
		public Node(T value, Node<T> next)
		{
			Next = next; Value = value;
		}
	}

I get a Parsing Error on this:

static List<string> availableCharacters = new List<string>();

does anyone know if you can Use the List on iPhone or do I need to find myself a way around this?

List<> works, given you correctly import the corresponding namespace

we are using following namespaces:

using System;
using System.Collections.Generic;
using UnityEngine;
using Object=UnityEngine.Object;
using Random=UnityEngine.Random;
using System.Collections;

Have you set the “API Compatibility Level” to “.NET 2.1” in the Unity Player settings (Edit > Project Settings > Player)?

Does that actually do anything? I can’t see a difference.

If you have a project that compiles fine under .NET 1.1, you probably won’t see any change.

However, if you attempt to compile with “using System.Collections.Generic” and you don’t have .NET 2.1 enabled in the Project Settings, you will certainly receive a compile error.

If you want to use Generics, you must enable .NET 2.1 in Unity.

Hmmm… I’m not usually a complainer… but come on. How hard it is for Mr or Mrs Unity to just say:

The following work (with compat level 2.1):

List<>
Dictionary<>

The following don’t work:

Queue<>
Stack<>

Am I missing something here? Shouldn’t someone out there know exactly what works and what doesn’t work and maybe put it in the manual so we don’t have to guess or experiment?

It would appear that Unity iPhone’s Generic support is partial due, at least in part, to this:

http://monotouch.net/Documentation/Limitations

That explains the inconsistencies, but it would have been nice if the release notes didn’t merely say “Generics support”