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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:45:28+00:00 2026-05-27T16:45:28+00:00

I have a dynamic say d. I am adding some properties to it by

  • 0

I have a dynamic say d. I am adding some properties to it by doing the following:

((IDictionary<String, Object>)d).Add("Propname", Propvalue);

Now, when i have properties that have the same name , this obviously throws an exception. I want to allow all the properties even if they are of the same name. So, the Dictionary is a wrong choice of Data Structure . I was thinking about the BAG equivalent of JAVA in C#. I could not find anything . What is the best way of going about this ?

  • 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-05-27T16:45:29+00:00Added an answer on May 27, 2026 at 4:45 pm

    You seem to be completely mixing several concepts.

    dynamic is a trigger that makes compiler generate late-bound code. Although it’s not verified in compile-time, it follows the same rules as the ordinary c# code. It cannot have multi-valued properties (can you have them wherever in .NET?). The only way is having a collection-valued property and adding new values to the collection. Something like this:

    dynamic obj = new ExpandoObject();
    obj.SomeProperty = new List<object>();
    
    obj.SomeProperty.Add(SomeValue);
    obj.SomeProperty.Add(AnotherValue);
    
    foreach(var somePropertyValue in obj.SomeProperty)
    {
      DoSmth(somePropertyValue);
    }
    

    dynamic has nothing to do with IDictionary or Lookup. A dynamic variable could be an arbitrary object, for instance an int value. If you use dynamic, it’s very strange that you cast it to the IDictionary interface. Just call Add and the DLR will resolve the method for you (like I did above).

    You should usually use dynamic only if you interoperate with a dynamic environment (script languages, COM, etc.). If you only work with c# code – throw away all the dynamic stuff and use regular .NET collections. It will both be statically verified by the compiler and work faster.

    For example, you could use a Dictionary<string, ICollection<object>> to have a dynamic dictionary:

    var dictionary = new Dictionary<string, ICollection<object>>();
    dictionary["SomeProperty"] = new List<object>();
    
    dictionary["SomeProperty"].Add(SomeValue);
    dictionary["SomeProperty"].Add(AnotherValue);
    
    foreach(var somePropertyValue in dictionary["SomeProperty"])
    {
      DoSmth(somePropertyValue);
    }
    

    UPDATE

    Oh, yes, you could try creating your own DynamicObject implementation that would put values into a bag upon property assignment instead of overwriting the existing value. Specifically, you should override the TrySetMember method. But first think if you really need all this complex stuff.

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

Sidebar

Related Questions

So let's say we have a domain object such as the following public class
Say I have a some links that uses the live method to load dynamic
I have a website in which the total dynamic content is very small (say
I have a dynamic page (say myFlashContainer.jsp) which renders different Flash content based on
Let's say I have this: dynamic foo = new Foobar(); And I have this:
I have a form generating dynamic cookie, say: webform-62[1234356] 62[1234356] can be any number.
Let's say I have a dynamic array that is populated on page load with
I have a Dynamic ExpandableListView. That is to say child are only load when
how do i create object-instances on runtime in python? say i have 2 classes:
I have the following form, and I am dynamically adding more dropdowns (exactly the

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.