Skip to content

One about foreach and type access #80647

@SimpleZDM

Description

@SimpleZDM

Description

This is an image
This is an image
I am a junior programmer.
I want you to debug the above problems, may not be as simple as you think.
This makes me very confused and I would like to have a.NET expert help explain.

Reproduction Steps

100%

Expected behavior

Cannot apply indexing with [] to an expression of type 'object'

Actual behavior

An exception will be thrown if the type is not public (Cannot apply indexing with [] to an expression of type 'object')
It's easy to understand that the runtime environment finds out that it's a protected type so it throws an exception but you can access it directly with foreach whether test is a protected type or not private

Regression?

An exception will be thrown if the type is not public (Cannot apply indexing with [] to an expression of type 'object')
It's easy to understand that the runtime environment finds out that it's a protected type so it throws an exception but you can access it directly with foreach whether test is a protected type or not private

Known Workarounds

An exception will be thrown if the type is not public (Cannot apply indexing with [] to an expression of type 'object')
It's easy to understand that the runtime environment finds out that it's a protected type so it throws an exception but you can access it directly with foreach whether test is a protected type or not private

Configuration

.net 6

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions