06 Feb 2024




Intermediate

The dynamic keyword in C# is used to declare variables whose types are not known until runtime. It enables you to bypass compile-time type checking and instead perform type checking at runtime, providing flexibility in scenarios where the type is determined dynamically or when interfacing with dynamic languages or APIs.

Here's a simple example illustrating the use of the dynamic keyword:

dynamic dynamicVariable = "Hello, world!";
Console.WriteLine(dynamicVariable.ToUpper()); // This works fine at runtime.

dynamicVariable = 10;
Console.WriteLine(dynamicVariable + 5); // This also works, and it prints 15.

In the above example, dynamicVariable can hold different types of values (a string and an integer), and the compiler doesn't perform type checking until runtime. This allows for flexibility in handling different types of data without explicitly specifying their types at compile time.

Here are some common use cases for the dynamic keyword:

  1. Interoperability with Dynamic Languages: When working with dynamic languages like Python or JavaScript through interop, using the dynamic keyword can simplify interactions by deferring type resolution until runtime.

    dynamic pythonObject = GetPythonObject(); // Assume GetPythonObject() returns a dynamic object from Python
    dynamic result = pythonObject.someMethod();
    Console.WriteLine(result);
    
  2. Working with COM Objects: When interacting with COM (Component Object Model) objects or APIs, the dynamic keyword can be useful because COM objects often expose properties and methods dynamically.

    dynamic comObject = Activator.CreateInstance(Type.GetTypeFromProgID("SomeCOMObject"));
    dynamic result = comObject.SomeMethod();
    Console.WriteLine(result);
    
  3. Late Binding: If you need to access members of objects without knowing their types at compile time, the dynamic keyword allows you to perform late binding, which resolves member invocations and accesses at runtime.

    dynamic obj = GetObjectFromExternalSource(); // Assume GetObjectFromExternalSource() returns a dynamic object
    dynamic result = obj.SomeMethod();
    Console.WriteLine(result);
    
  4. Simplified Reflection: In some cases, the dynamic keyword can provide a more concise alternative to reflection for accessing members of objects when you know the member names but not their types until runtime.

    dynamic obj = GetObjectFromUnknownType(); // Assume GetObjectFromUnknownType() returns a dynamic object
    dynamic value = obj.SomeProperty;
    Console.WriteLine(value);
    
  5. Parsing Dynamic Data: When dealing with data from external sources, such as JSON or XML, the dynamic keyword can simplify data access by allowing you to navigate through the data structure without needing to define corresponding .NET types.

    dynamic jsonData = JsonConvert.DeserializeObject<dynamic>("{\"name\": \"John\", \"age\": 30}");
    Console.WriteLine(jsonData.name); // Accessing properties dynamically
    Console.WriteLine(jsonData.age);
    
c-sharp
dynamic