Explicit IEnumerable<T> implementations
Hi
The collections in
This is fine but when iterating the collection in a foreach e.g.
the compiler emits an information diagnostic:
Given the implementation nothing will fail but it would be nice if the collections publicly implemented the generic
Thanks
The collections in
dotnet/interopcollections
explicitly implement GetEnumerator
on the generic IEnumerable<T>
and publicly/implicitly implement the nongeneric version:
IEnumerator<TransferEventArgs> IEnumerable<TransferEventArgs>.GetEnumerator() { return _helper.GetEnumerator(); } public IEnumerator GetEnumerator() { return _helper.GetEnumerator(); }
This is fine but when iterating the collection in a foreach e.g.
foreach (TransferEventArgs upload in synchronizationResult.Uploads) { }
This is because the compiler is lowering theIDE0220 'foreach' statement implicitly converts 'object' to 'WinSCP.TransferEventArgs'. Add an explicit cast to make intent clearer, as it may fail at runtime
foreach
using the nongeneric GetEnumerator
and iterating the items as object type.
Given the implementation nothing will fail but it would be nice if the collections publicly implemented the generic
GetEnumerator
and explicitly implemented the nongeneric version, e.g.
diff --git a/dotnet/interopcollections/TransferEventArgsCollection.cs b/dotnet/interopcollections/TransferEventArgsCollection.cs index 5d5a4dc4c..a8e435a51 100644 --- a/dotnet/interopcollections/TransferEventArgsCollection.cs +++ b/dotnet/interopcollections/TransferEventArgsCollection.cs @@ -66,7 +66,7 @@ namespace WinSCP #region IEnumerable<SessionRemoteException> Members - IEnumerator<TransferEventArgs> IEnumerable<TransferEventArgs>.GetEnumerator() + public IEnumerator<TransferEventArgs> GetEnumerator() { return _helper.GetEnumerator(); } @@ -75,7 +75,7 @@ namespace WinSCP #region IEnumerable Members - public IEnumerator GetEnumerator() + IEnumerator IEnumerable.GetEnumerator() { return _helper.GetEnumerator(); }
Thanks