Dynamic Objects in C# with Code Samples

Introduction

The C# language has many powerful features, including the ability to create and manipulate dynamic objects. This post will explain the concept of dynamic objects in C#, provide code samples to demonstrate their use, and discuss the benefits of dynamic objects.

What are Dynamic Objects in C#?

Dynamic objects in C# are objects that allow users to modify their properties and methods at runtime. They have no predefined structure, meaning that their members and methods can be added and removed as needed. This flexibility makes them ideal for scenarios where the structure of the object can change depending on the environment or user input.

Benefits of Using Dynamic Objects in C#

Dynamic objects in C# offer a number of advantages over standard objects. They can be used to quickly prototype an application without having to set up a complex object structure. Additionally, they can be more efficient when dealing with large amounts of data as they don’t need to be initialized at the start. Finally, dynamic objects can also be used to access and manipulate data from other sources, such as databases or web services.

Code Samples

The following code samples demonstrate the use of dynamic objects in C#. The first example shows how to create a dynamic object and add a property to it:

dynamic myObj = new ExpandoObject();
myObj.Name = "John";


The second example shows how to access the properties of a dynamic object:

dynamic myObj = new ExpandoObject();
myObj.Name = "John";

string name = myObj.Name; // name = "John"


Finally, the third example shows how to invoke a method of a dynamic object:

dynamic myObj = new ExpandoObject();
myObj.SayHello = (string name) => {
    Console.WriteLine($"Hello, {name}!");
};

myObj.SayHello("John"); // Prints "Hello, John!"


Conclusion

Dynamic objects in C# provide an effective way to quickly prototype applications and access data from other sources. They also offer a number of benefits over standard objects, such as increased efficiency and flexibility. By using the code samples provided in this post, users can begin to take advantage of dynamic objects in their own projects.

Advertisement

Next js experiment

Tried out an app with Next js with the help of an YouTube tutorial video. The app contains basic features like loading data from external api, single level routing, nested routing, layout templates, Hyperlink navigations, Card styles. Here is an outcome. https://next-experiment-six.vercel.app

Thanks @traversymedia

This video is really helpful. https://youtube.com/watch?v=mTz0GXj8NN0&ab_channel=TraversyMedia…

I shall try the Udemy course as well shortly.

Deployed the source in vercel by following the official docs steps. Live: https://next-experiment-six.vercel.app

Its quick and straight forward. I’m yet to figure out – how to trigger the build & deployment in vercel on each commit or merge.

Planning to add footer and some real-time data + images to see on how image rendering & optimisations happening.

#nextjs#react#vercel

Reference

Source of this app is available in the following path

https://github.com/PandiyanCool/next-experiment