Unplanned
Last Updated: 08 Jan 2020 15:54 by ADMIN
Christoph
Created on: 04 Jan 2020 12:30
Type: Bug Report
0
Direct use of enumerator incorrectly translated into foreach (C# and VB)

Compile the following code:

using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace BugReport {

	public class CustomObservableCollection<T> : ObservableCollection<T> {

		public void AddRange<S>(IEnumerable<S> list) where S : T {
			using (IEnumerator<S> enumerator = list.GetEnumerator()) {
				while (enumerator.MoveNext()) {
					base.Add(enumerator.Current);
				}
			}
		}

	}

}

Open the generated assembly in Just Decompile and look at the result. In my version (2019.1.118.0) I get the following incorrect (not compilable) results:

C#:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace BugReport
{
    public class CustomObservableCollection<T> : ObservableCollection<T>
    {
        public CustomObservableCollection()
        {
        }

        public void AddRange<S>(IEnumerable<S> list)
        where S : T
        {
            foreach (S  in list)
            {
                base.Add((T)(object));
            }
        }
    }
}

VB:


Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel

Namespace BugReport
    Public Class CustomObservableCollection(Of T)
        Inherits ObservableCollection(Of T)
        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub AddRange(Of S As T)(ByVal list As IEnumerable(Of S))
            For Each  As S In list
                MyBase.Add(DirectCast(, T))
            Next
        End Sub
    End Class
End Namespace

 

0 comments