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 9065439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:30:12+00:00 2026-06-16T16:30:12+00:00

How many times we declare a simple class or struct to hold a few

  • 0

How many times we declare a simple class or struct to hold a few properties only to use them only one time when returned by a method. Way too many times I think, thankfully we always have anonymous objects which can be declared in place at run time.

With that thought in mind I would like to know how I can declare one array of such anonymous objects.

Example:

var Employee = new { ID = 5, Name= "Prashant" };

This created a anonymous object with two properties, one being integer and another string.

All good here, but how should I declare a array of this kind of object, and how would you recommend iterating through it with both a for and foreach loops.

The foreach loop is really the problem I think, because foreach loops expect a declared type. Still there might be a away, if there is I would to know it too of course.

  • 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-16T16:30:13+00:00Added an answer on June 16, 2026 at 4:30 pm

    [edit – updated to show population, basic enumeration, etc]

    As @Eve says, LINQ is your friend here; as a general rule of thumb, don’t try passing anonymous types around – you can, if you’re clever – but it’s a HUGE pain in the butt to deal with them outside of the context/scope they were declared.

    To whit, I decided to see in what ways one could “declare an array of an anonymous type” as a fun thought experiment, and came up with these:

    (note: the “Dump” is due to this being written in LINQPad)

    // Our anonymous type sequence
    var anonymousEnumerable = Enumerable
            .Range(0, 10)
            .Select(i => new { ID = i, Text = i.ToString() });
    var enumerableCount = anonymousEnumerable.Count();
    var anonymousType = anonymousEnumerable.First().GetType();
    
    // Option #1 - declare it as dynamic, i.e., anything goes
    dynamic[] asDynamicArray = new dynamic[enumerableCount];
    foreach(var tuple in anonymousEnumerable.Select((item, i) => Tuple.Create(i, item)))
    {
        asDynamicArray[tuple.Item1] = tuple.Item2;
    }
    
    // Let's go the IEnumerable route
    foreach (var asDynamic in asDynamicArray)
    {
        Console.WriteLine("ID:{0} Text:{1}", asDynamic.ID, asDynamic.Text);
    }
    
    // Lowest common denominator: *everything* is an object
    object[] asObjectArray = new object[enumerableCount];
    foreach(var tuple in anonymousEnumerable.Select((item, i) => Tuple.Create(i, item)))
    {
        asObjectArray[tuple.Item1] = tuple.Item2;
    }
    
    // Let's iterate with a for loop - BUT, it's now "untyped", so things get nasty
    var idGetterMethod = anonymousType.GetMethod("get_ID");
    var textGetterMethod = anonymousType.GetMethod("get_Text");
    for(int i=0;i < asObjectArray.Length; i++)
    {
        var asObject = asObjectArray[i];
        var id = (int)idGetterMethod.Invoke(asObject, null);
        var text = (string)textGetterMethod.Invoke(asObject, null);
        Console.WriteLine("ID:{0} Text:{1}", id, text);
    }
    
    // This is cheating :)
    var letTheCompilerDecide = anonymousEnumerable.ToArray();
    foreach (var item in letTheCompilerDecide)
    {
        Console.WriteLine("ID:{0} Text:{1}", item.ID, item.Text);
    }
    
    // Use reflection to "make" an array of the anonymous type
    var anonymousArrayType = anonymousType.MakeArrayType();
    var reflectIt = Activator.CreateInstance(
              anonymousArrayType, 
              enumerableCount) as Array;    
    Array.Copy(anonymousEnumerable.ToArray(), reflectIt, enumerableCount);  
    
    // We're kind of in the same boat as the object array here, since we
    // don't really know what the underlying item type is
    for(int i=0;i < reflectIt.Length; i++)
    {
        var asObject = reflectIt.GetValue(i);
        var id = (int)idGetterMethod.Invoke(asObject, null);
        var text = (string)textGetterMethod.Invoke(asObject, null);
        Console.WriteLine("ID:{0} Text:{1}", id, text);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Many times I needed a set of pointers. Every time that happens, I end
Many times I use 'mqsc' for create MQ queue manager from script files but
Many times i run time consuming PHP scripts that echo status updates like 'batch
I have one costly function that gets called many times and there is a
here i have one function which is call many times in one application this
Many times I get quick page design inputs from business/clients which will be finally
Many times I work with optimized code (sometimes even involving vectorized loops), which contain
Many times when I lose connection to my development server, I just log back
Many times I see statements like following Y.CustomApp.superclass.render.apply(this, arguments); I know how apply works.
How many times have you seen someone trying to Log the command I run

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.