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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:46:34+00:00 2026-06-13T18:46:34+00:00

I would like to create a anonymous type that can be iterated, takes a

  • 0

I would like to create a anonymous type that can be iterated, takes a string and a string array. The goal is to create a list of groups and each group contains persons:

I would like to know if something like this Pseudo-Code is possible:

var myAnoty =new List<string, string[]>();
myAnoty.add("Programmers", ["Linus", "Bill", "Anders"]);
myAnoty.add("Users", ["John", "Melissa", "Bert"]);

As Muzz pointed out in the comments my pseudo code above is not using an Anonymous Type. Reed Copsey answer below is using them: var m = new { Group = "Men", Members = new[] { "John", "Tom" } }

Working demo without using anonymous types

The following code works in LinqPad:

void Main()
{
    List<Object> myAnoties = new List<Object>();
    myAnoties.Add(new Anoty {Group="Men",   Member=new string[]{"John","Tom"}} );
    myAnoties.Add(new Anoty {Group="Women", Member=new string[]{"Eve","Anna"}} );
    myAnoties.Dump();
}

// Define other methods and classes here
class Anoty{
    public string Group{get; set;}
    public string[] Member{get; set;}       
}

Not working using anonymous types

The following code uses anonymous types. But somehow the string array will not work with anonymous types.

var m = new { Group= "Men", Member=string[]{"John","Tom"}};
var w = new { Group= "Women", Member=string[]{"Eve","Anna"}};
List<object> myAnoties = new List<object>();
myAnoties.Add(m);
myAnoties.Add(w);

Is there any way to get something like my Pseudo-Code var x = new List<string,string[]>() or something with anonymous types (like above) going?

Thanks

  • 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-13T18:46:35+00:00Added an answer on June 13, 2026 at 6:46 pm

    You can accomplish something like your pseudo-code:

    var myAnoty = new Dictionary<string, string[]>();
    myAnoty.Add("Programmers", new[] { "Linus", "Bill", "Anders" });
    myAnoty.Add("Users", new[] { "John", "Melissa", "Bert" });
    

    Or, if you want a List<T>:

    var myAnoty = new List<Tuple<string, string[]>>();
    myAnoty.Add(Tuple.Create("Programmers", new[] { "Linus", "Bill", "Anders" }));
    myAnoty.Add(Tuple.Create("Users", new[] { "John", "Melissa", "Bert" }));
    

    You can also use arrays within anonymous types:

    var m = new { Group = "Men", Members = new[] { "John", "Tom" } } 
    var w = new { Group = "Women", Members = new[] { "Eve", "Anna" } };
    

    However, assigning this into a List<object> will make this effectively unusable, as you’ll have no way to access the items by name anymore. You could place them into a List<dynamic> and access them, however.

    That being said, I’d strongly recommend reconsidering the goal of using anonymous typing entirely. Using named types is typically preferential for situations where you’re going to store the objects. Anonymous types save a bit of typing in this scenario, but add a huge maintenance cost as they add a lot of complexities, and you lose a lot of safety (ie: not having proper constructors, etc). The extra time required to create a custom type is typically far smaller than the debugging and maintenance costs of using anonymous types over time in scenarios where you will persist the values.

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

Sidebar

Related Questions

I would like to create a (non-anonymous) function that sorts an array of objects
I have a cell array of anonymous function handles, and would like to create
I would like to create a c++ type that mimic the build-in type exactly.
I would like to expose a function that can take an optional anonymous method
i would like create a array of structure which have a dynamic array :
I would like to create a json object to send as a post array,
I would like to create a class that runs something (a runnable) at regular
I would like to create an application which can learn to classify a sequence
Is it possibly to create a class-level anonymous type? Like, class MyClass { readonly
I would like to create this shape using just css. I am pretty sure

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.