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

  • Home
  • SEARCH
  • 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 7856537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:44:29+00:00 2026-06-02T20:44:29+00:00

Say I define a variable like this: var o = new { RBI =

  • 0

Say I define a variable like this:

var o = new { RBI = 108, Name = "Roberto Alomar" };

I can do something like:

Console.WriteLine("{0}", o);

But if I try:

foreach (var i in o) {
    Console.WriteLine("{0}", o[i]);
}

I get an error:

foreach statement cannot operate on variables of type 'AnonymousType#1' because 'AnonymousType#1' does not contain a public definition for 'GetEnumerator'

So how does it work under the hood? I’d think that a method for turning an object into a string would have to loop through all the properties to accomplish the task. Is there some special method that allows this to happen, or am I misunderstanding how this works?

  • 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-06-02T20:44:30+00:00Added an answer on June 2, 2026 at 8:44 pm

    How does it work under the hood? I’d think that a method for turning an object into a string would have to loop through all the properties to accomplish the task.

    Your assumption is that the implementation of ToString is shared between all instances of all anonymous types; that, for example, there is some helper that is logically something like you would do in JavaScript:

    var s = "";
    for (property in this)
       s += property + ":" + this[property];
    

    This assumption is wrong; there is no single one-size-fits-all implementation of ToString for anonymous types. Rather, the compiler knows what all the properties of the anonymous method are and so it generates a brand-new custom implementation of ToString for every distinct anonymous type.

    In C#, the foreach loop does not do what the for-in loop does in JavaScript. The C# loop enumerates the members of a collection. The JS loop enumerates the properties of an object.

    If you want to enumerate the properties of an object in C# you can do that, it just takes a bit more work:

    var s = "";
    foreach (PropertyInfo propertyInfo in this.GetType().GetProperties())
       s += propertyInfo.Name + ":" + propertyInfo.GetValue(this).ToString();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a LINQ-to-XML query that generates an anonymous type like this: var
Lets say I have a class like this: Class User { var $id var
I have some code like this: if (condition) { var variable = blah; }
Let's say that I define a ClassA , that implements NSCoding protocol (this class
Say I want to define that an URI such as: myapp://path/to/what/i/want?d=This%20is%20a%20test must be handled
Say in my web.xml file, I define a servlet like so: <url-pattern>/MyURL/*</url-pattern> How do
I want to define some basic styles that I can combine. Let's say I
I've noticed something odd about user-defined variables: Lets say I have this table: num_table
If I have a javascript object/assoc. array defined like this: function somefunction(options) { var
Say I have a Javascript class defined and instantiated like this: Demo = function()

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.