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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:17:49+00:00 2026-06-11T19:17:49+00:00

Is there a good existing or upcoming alternative in C# to declaring data structures

  • 0

Is there a good existing or upcoming alternative in C# to declaring data structures in methods? It’s possible to use anonymous types, but there are difficulties with declaring them. Let’s say I have a hypothetical class:

class ThingsManager
{
    private void DoThings(IEnumerable<Thing> things)
    {
        var thingLocations = new Dictionary<string, string>();

        foreach(var thing in things)
        {
            // some complicated logic and checks for current thing;
            // if current thing satisfies all conditions:

            var thingName = thing.Name;
            var thingLocation = location; // taken somewhere from upper lines
            thingLocations.Add(thingName, thingLocation);
        }

        // ... later

        foreach(var thingLocation in thingLocations)
        {
            // here I don't know what is the key and what does the value mean.
            // I could use Linq and anonymous types, but sometimes it is clearer 
            // to use foreach if the logic is complicated
        }
    }
}

Now, what I’d like to see:

class ThingsManager
{
    private void DoThings(IEnumerable<Thing> things)
    {
        struct ThingLocations
        {
            string ThingName {get;set;}
            string Location {get;set;}
        }

        var thingLocations = new List<ThingLocations>();

        foreach(var thing in things)
        {
            // some complicated logic and checks for current thing;
            // if current thing satisfies all conditions:

            var thingName = thing.Name;
            var thingLocation = location; // taken somewhere from upper lines
            thingLocations.Add(new ThingLocation(thingName, thingLocation));
        }

        // ... later

        foreach(var thingLocation in thingLocations)
        {
            // now here I can use thingLocation.ThingName  
            // or thingLocation.Location
        }
    }
}

I could also declare the structure in the class, but it doesn’t make sense to use it anywhere except in my function. It would be better if my function were the only place where I could use this data structure. I’m looking for a better way to handle such situations, or at least be able to declare anonymous types.

  • 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-11T19:17:50+00:00Added an answer on June 11, 2026 at 7:17 pm

    Anonymous types would help with the naming aspect, but you would have to translate the input into your anonymous type and the type would remain internal to the method scope.

    // Assumes thingLocation comes from somewhere...
    var thingLocations = things
        .Select(t => new { ThingName = t.Name, Location = new ThingLocation(t.Name, thingLocation) } );
    

    It is done using the Select extension method in order to project to an anonymous type.

    You can declare anonymous types without linq, but you will find it annoying trying to add those to lists / dictionaries:

    var me = new { Name = "Adam", Age = 27 };
    

    I’m going on record to say that I wouldn’t take this approach, personally I’d either use anonymous types, a Tuple<string, string>, or a custom type.

    Failing all of that, and if you don’t mind firing up the DLR, you could use an ExpandoObject:

        class Thing
        {
            public string Name;
        }
    
        static void Main(string[] args)
        {
            var things = new List<Thing>() { new Thing { Name = "Adam" } };
            var thingLocations = new List<dynamic>();
    
            foreach (var thing in things)
            {
                dynamic location = new ExpandoObject();
                location.Name = thing.Name;
                location.Location = "here";
    
                thingLocations.Add(location);
            }
    
            // ... later
    
            foreach(var thingLocation in thingLocations)
            {
                Console.WriteLine(thingLocation.Name);
                Console.WriteLine(thingLocation.Location);
            }
    
            Console.ReadLine();
        }
    

    This allows you to dynamically add properties as you need them by declaring them on the spot. You can then use these later because ExpandoObject provides the plumbing to the DLR when the DLR asks for a member by name.

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

Sidebar

Related Questions

Is there a good C library that I can use in my client application
Are there any good technical solutions for extremely long term archiving of data, for
Is there an existing good example, or how should one approach creating a basic
Are there any good tools for visualising a pre-existing database schema? I'm using MySQL
is there a good way to duplicate the existing values in an array and
Is there a good gem to track/log user activities on the site? Like when
Are there some good resources tutorials or anyone has tried to implement a Capcha
Is there any good library which supports WebSockets and is compatible with GWT? Ideally,
Are there any good technical comparisons available between the various game development options available.
Is there a good C library for graph theoretic manipulations? I particularly need to

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.