Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

RH

Excellent thanks very much
martin

Thanks for the details. I'll look into it.
RH

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 :-)
martin

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.
RH

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