General
Unity version: noticed in 2022.3, but should be present in any Unity version
Fish-Networking version: 4 and up
Discord link: None
Description
The return type of the public GetEnumerator() method in SyncList returns the non-generic IEnumerator instead of the generic IEnumerator<T>. This makes type of e in foreach (var e in syncList) an object, which is inconvenient in codebases that use var. This grows into a larger issue when explicitly specifying the iteration type of a SyncList of any value type: foreach (ValueType e in syncList) results in a hidden boxing conversion of object to ValueType at each iteration.
Replication
private readonly SyncList<int> list = new();
public void Example() {
foreach (var e in list) { // e is boxed as object, which is an unnecessary allocation
int x = e + 1; // This will not compile because e is boxed as an object
}
}
Expected behavior
Ideally, the public GetEnumerator() of SyncList would return IEnumerator<T>:
public IEnumerator<T> GetEnumerator() => Collection.GetEnumerator();
I have been making this change in my projects since updating to FishNet 4 and it works well. Thank you for making this excellent library!
General Unity version: noticed in 2022.3, but should be present in any Unity version Fish-Networking version: 4 and up Discord link: None
Description The return type of the public
GetEnumerator()
method in SyncList returns the non-genericIEnumerator
instead of the genericIEnumerator<T>
. This makes type ofe
inforeach (var e in syncList)
anobject
, which is inconvenient in codebases that usevar
. This grows into a larger issue when explicitly specifying the iteration type of aSyncList
of any value type:foreach (ValueType e in syncList)
results in a hidden boxing conversion ofobject
toValueType
at each iteration.Replication
Expected behavior Ideally, the public
GetEnumerator()
ofSyncList
would returnIEnumerator<T>
:public IEnumerator<T> GetEnumerator() => Collection.GetEnumerator();
I have been making this change in my projects since updating to FishNet 4 and it works well. Thank you for making this excellent library!