c sharp

What is a Dynamic Object in C#

In C#, a dynamic object is a type that allows you to specify the type of object at runtime instead of compile-time. It enables you to create code that can interact with objects whose type is unknown at compile time. This can be useful when working with objects that are created dynamically, such as those returned from a web service or a database query. In this article, we will discuss dynamic objects in C# and provide syntax and full code examples to demonstrate their use.

What is Dynamic Object in C#

In C#, a dynamic object was introduced in C# 4.0 and is often used to interact with APIs or systems that rely on dynamic languages or object models.

A dynamic object can have its properties and methods defined and invoked at runtime, instead of being predefined during the compilation process. This enables developers to write code that is more flexible and adaptable to changes in the runtime environment. In C#, you can create a dynamic object using the dynamic keyword, here is the syntax for creating a dynamic object:

dynamic dynamicObject = new ExpandoObject();

In this example, we are creating a new dynamic object using the ExpandoObject class. The dynamic keyword tells the compiler that we don’t know the type of the object at compile-time and that it should be determined at runtime:

using System;

using System.Dynamic;

class Program
{
    static void Main(string[] args)
    {
        dynamic person = new ExpandoObject();
        person.Name = "SAM BOSH;
        person.Age = 30;

        Console.WriteLine("Name: {0}, Age: {1}", person.Name, person.Age);

        person.SayHello = new Action(() => Console.WriteLine("Hello!"));
        person.SayHello();
    }
}

Here, we use the ExpandoObject class to construct a brand-new dynamic object. Next, the attributes Name and Age are added to the object, and their values are set.

Next, we add a method to the object called SayHello. We use the Action delegate to define the method and set it to the SayHello property of the dynamic object. We then call the SayHello method, which writes “Hello!” to the console.

The output of the code shows that the dynamic properties Name and Age can be accessed using dot notation, just like any other object in C#. Also, the dynamic property SayHello can be invoked like any other method or delegate. The use of the ExpandoObject class and the dynamic keyword allows for dynamic property creation and delegation invocation, making the code more flexible and adaptable.

Limitation of Dynamic Object in C#

Dynamic objects in C# can have limitations such as slower performance, more difficult debugging, lack of Intellisense, potential for runtime errors, and reduced type safety. It’s important to use dynamic objects judiciously and be aware of their limitations.

Conclusion

Working with objects whose types are unknown until runtime is possible thanks to C#’s dynamic objects. This can be useful when working with objects that are created dynamically, such as those returned from a web service or a database query. By using the dynamic keyword, you can create objects at runtime and add properties and methods to them as needed. The syntax for creating a dynamic object is simple, and once created, you can work with the object just like any other object in C#.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.