DEV Community

Afshar
Afshar

Posted on

1

Dynamic features of the C#

I am a long term user of C# language. C# have been a static language for most of its life time. Recently, I have used JavaScript a lot. Dynamic nature of the JavaScript became handy for me. It is enjoyable when I am not forced to define an entire DTO class when I want to receive or pass a series of the parameters in the JavaScript. All of all is to define members in a JSON format.

When you get used to it, you like to behave alike in the C#. Fortunately, new C# have some dynamic features. It has keyword dynamic, and it contains ExpandoObject.

In my case, I used dynamic features for easily generating a JSON output. By my default style, I should created a series of DTOs, then populate them in a nested loop from database. However, dynamic feature let me exclude all redundant DTO classes and, simply inject data by extending the ExpandoObject.

At first glance, I had confusion about the order in which items would be added to the ExpandoObject or List<dynamic>. But by exercise and test, I found the correct solution.

Next step, I will try to use dynamic feature to receive binding models from the API. This is another area which generates a lot of less used DTOs. By now, I don't know about any other usage of dynamic in the C# and ASP.NET, but I am open for it.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay