C# Tip: Convert ExpandoObjects to IDictionary

Read Time:1 Minute, 22 Second

In C#, ExpandoObjects are dynamically-populated objects without a predefined shape.

Name and Age are not part of the definition of ExpandoObject: they are two fields I added without declaring their type.

This is a dynamic object, so I can add new fields as I want. Say that I need to add my City: I can simply use

without creating any field on the ExpandoObject class.

Now: how can I retrieve all the values? Probably the best way is by converting the ExpandoObject into a Dictionary.

Create a new Dictionary

Using an IDictionary makes it easy to access the keys of the object.

If you have an ExpandoObject that will not change, you can use it to create a new IDictionary:

Notice that we use the ExpandoObject to create a new IDictionary. This means that after the Dictionary creation if we add a new field to the ExpandoObject, that new field will not be present in the Dictionary.

Cast to IDictionary

If you want to use an IDictionary to get the ExpandoObject keys, and you need to stay in sync with the ExpandoObject status, you just have to cast that object to an IDictionary

This works because ExpandoObject implements IDictionary, so you can simply cast to IDictionary without instantiating a new object.

Here’s the class definition:

Wrapping up

Both approaches are correct. They both create the same Dictionary, but they act differently when a new value is added to the ExpandoObject.

Can you think of any pros and cons of each approach?

Happy coding!

🐧

Source: https://www.code4it.dev/csharptips/expandoobject-to-dictionary

Tag Cloud

Java Java Logical Programs OTP Generation in Java python Recursion youtube video ASCII Upper and Lower Case blockchain javascript graph learn to code software development Successful Software Engineers breadth first search Java Array Programs Java Programs Uncategorized android ios programming kotlin web-development django data sql cybersecurity database swiftui serverless aws swift rust react background-position gradients loader mask grid nth-child pseudo elements indieweb WordPress Print Array without brackets C++ factorial Java String Programs Final Keyword Static Variable Axie Infinity Cryptokitties NFT games tool inserting MISC Tips Codes python code python projects python3 system info python project Bigginers How to Do Integrations Payment Gateways PHP checkout page in php Implement stripe payment gateway in Step by step in PHP integrate stripe gatway in php mysql payment gateway integration in php step by step payment gateway integration in php step by step with source code payment gateway integration in website PHP Integrate Stripe Payment Gateway Tutorial PHP shopping cart checkout code shopping cart in php stripe php checkout PHP/MySQL/JSON best international payment gateway does google pay accept international payments how to accept international payments in india paytm payment gateway razorpay codeigniter github razorpay custom checkout github razorpay get payment details razorpay integration in codeigniter github razorpay international payments Razorpay payment gateway integration in CodeIgniter razorpay payment gateway integration in php code Razorpay payment gateway integration with PHP and CodeIgniter Razorpay payment gateway setup in CodeIgniter Library & Frameworks Tips & Tricks UI/UX & Front-end coding birds online html code for google sign in login with google account in PHP login with google account using javascript login with google account using javascript codeigniter login with google account using php login with google account using php source code
Useful Tools for Creating AVIF Images Previous post Useful Tools for Creating AVIF Images
Backup Plugin Battle: Which is the Best WordPress Backup Plugin in 2022? Next post Backup Plugin Battle: Which is the Best WordPress Backup Plugin in 2022?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.