[Common] Avoid GC in DeviceCollection
authorthefiddler <stapostol@gmail.com>
Sat, 20 Sep 2014 15:10:05 +0000 (17:10 +0200)
committerthefiddler <stapostol@gmail.com>
Sat, 20 Sep 2014 15:10:05 +0000 (17:10 +0200)
`DeviceCollection.GetEnumerator()` now returns a struct IEnumerable<T>
directly to avoid boxing.

Additionally, we can now use `DeviceCollection[int]` as a shortcut to
`FromIndex(int)`.

Source/OpenTK/Platform/DeviceCollection.cs

index 804dac9..02e7492 100644 (file)
@@ -46,7 +46,7 @@ namespace OpenTK.Platform
 
         #region IEnumerable<T> Members
 
-        public IEnumerator<T> GetEnumerator()
+        IEnumerator<T> IEnumerable<T>.GetEnumerator()
         {
             return Devices.GetEnumerator();
         }
@@ -64,6 +64,17 @@ namespace OpenTK.Platform
 
         #region Public Members
 
+        // This avoids boxing when using foreach loops
+        public List<T>.Enumerator GetEnumerator()
+        {
+            return Devices.GetEnumerator();
+        }
+
+        public T this[int index]
+        {
+            get { return FromIndex(index); }
+        }
+
         /// \internal
         /// <summary>
         /// Adds or replaces a device based on its hardware id.