Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7546695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T09:15:55+00:00 2026-05-30T09:15:55+00:00

In the following code, For Each item in MyCollection … Next What does the

  • 0

In the following code,

For Each item in MyCollection
...
Next

What does the compiler use to determine the type of item?

For example let say I have this class, which is inheriting a non generic collection,

Public Class BaseDataObjectGenericCollection(Of T)
    Inherits BaseDataObjectCollection
End Class

A for each loop still infers the Item type as Object. How would I have to modify the above class to make the type inference work?

Edit: Per Beatles1692’s answer, Implementing IEnumerator(Of T) kinda works. The base class already has a GetEnumerator function, inherited from CollectionBase, so I my implementation looked like this,

    Public Function GetEnumerator1() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
        Return MyBase.Cast(Of T)().GetEnumerator
    End Function

However, the for loop still infers the type as object. But, if I change the interface implementation to this,

    Public Shadows Function GetEnumerator() As System.Collections.Generic.IEnumerator(Of T) Implements System.Collections.Generic.IEnumerable(Of T).GetEnumerator
        Return MyBase.Cast(Of T)().GetEnumerator
    End Function

That works, the for loop gets the type inference correct. So I guess the question is now, does For Each just look for a function called GetEnumerator ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T09:15:56+00:00Added an answer on May 30, 2026 at 9:15 am

    Well, there’s only one place to go for a question like this. The spec!

    Section 10.9.3 discusses For Each statements. According to it:

    [if] local variable type inference is being used, then the identifier defines a new local variable whose scope is the entire For loop and whose type is the element type of the collection (Object if the enumerator expression is typed as Object).

    “collection” here seems vague, but it’s precisely defined on the next page. Essentially, the type must have a GetEnumerator() call, and this enumerator must (a) have a MoveNext() method that returns a boolean type, and (b) have a Current property. The type of the Current property is the type that will be inferred by the compiler. Note it actually has nothing to do with IEnumerator or IEnumerable…you just have to fit the prescribed pattern. Consider this code:

    Option Infer On
    
    Public Module M
        Sub Main()
            For Each x In New SomeClass()
    
            Next
        End Sub
    End Module
    
    Public Class SomeClass
        Public Function GetEnumerator() As MyEnumerator
            Return New MyEnumerator()
        End Function
    End Class
    
    
    Public Class MyEnumerator
        Public ReadOnly Property Current As Integer
            Get
                Return 42
            End Get
        End Property
    
        Public Function MoveNext() As Boolean
            Return True
        End Function
    End Class
    

    The type of “x” in the Sub Main() is Integer, since the Current property returns Integer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several blocks of the following code that each use there own matrix.
I have the following code which I use to match fancybox possible elements: $('a.grouped_elements').each(function(){
In the following code I'm rendering a list of projects, each item of which
I encountered the following Ruby code: class MyClass attr_accessor :items ... def each @items.each{|item|
The following code in my cart is skipping the loop for each item and
I am using the following code to override and draw each list item within
the following code (from a database object created for each new view): -(void)update:(NSString *)queryText{
The following code returns the error from the For Each loop. I have similar
The following code makes a list of names and 'numbers' and gives each person
If I have the following code : threads = [] (1..5).each do |i| threads

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.