Explicit IEnumerable<T> implementations

Advertisement

RH
Guest

Explicit IEnumerable<T> implementations

Hi

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)
{
 
}
the compiler emits an information diagnostic:
IDE0220 'foreach' statement implicitly converts 'object' to 'WinSCP.TransferEventArgs'. Add an explicit cast to make intent clearer, as it may fail at runtime
This is because the compiler is lowering the 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

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
40,567
Location:
Prague, Czechia

Re: Explicit IEnumerable<T> implementations

Thanks. What version of VS/.NET/C# does emit this warning?

I'll have to test what impact would this change have on COM interop.

Reply with quote

RH
Guest

Thanks for the reply, and sorry for my late one.

This example is in .NET 7 / VS2022 17.5.4 (which uses Roslyn 4.5.0, I assume that's where it comes from).

Full repro:
<Project Sdk="Microsoft.NET.Sdk">
 
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
  </PropertyGroup>
 
  <ItemGroup>
    <PackageReference Include="WinSCP" Version="5.21.8" />
  </ItemGroup>
 
</Project>
using WinSCP;
 
namespace ConsoleApp1;
 
internal class Program
{
    static void Main()
    {
        Session session = new();
        // Information diagnostic on next line
        foreach (RemoteFileInfo _ in session.ListDirectory("").Files)
        {
        }
    }
}
Again, everything works nicely, it's just a nice to have :-)

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum