Language: EN

csharp-dynamics

Dynamics in C#

The dynamic type is a feature introduced in C# 4.0 that allows for dynamic programming. It allows us to resolve types and members at runtime instead of at compile time.

In other words, the dynamic type skips static type checking at compile time and delegates the resolution of types and members to runtime.

This means that operations on dynamic type variables are not validated until the program is running. This allows for great flexibility in certain programming scenarios.

However, it also has many detractors. It means skipping a bit of the strong typing guidelines, which many developers do not like (in fact, it was not particularly successful, and it is somewhat out of use)

Basic syntax

The dynamic type is declared similarly to other types in C#, using the dynamic keyword as the type.

dynamic variable = 10;
variable = "Hello, World!";
variable = new List<int> { 1, 2, 3 };

In this example, variable can hold different types of values, and the actual type of the variable is resolved at runtime.

Operations with dynamic

When performing operations with a dynamic type variable, the compiler does not perform any type checking. Instead, the operations are resolved at runtime.

dynamic value = 10;
Console.WriteLine(value + 5); // Valid operation, output: 15

value = "Hello";
Console.WriteLine(value + " World!"); // Valid operation, output: Hello World!

In the first case, value is treated as an integer, while in the second case, it is treated as a string.

Exception handling

Since dynamic operations are resolved at runtime, it is especially important to watch out for exceptions that may occur due to type errors.

try
{
    dynamic value = "text";
    int number = value; // Will cause a runtime exception
}
catch (RuntimeBinderException ex)
{
    Console.WriteLine($"Error: {ex.Message}");
}

Advantages and disadvantages of using dynamic

Disadvantages

  • Performance: dynamic operations may be slower due to the need to resolve types at runtime.
  • Loss of type safety: Using dynamic skips type checks at compile time, which can lead to runtime errors that would be avoidable with statically defined types.
  • Less clarity: Code with dynamic can be harder to understand and maintain due to the lack of type information at compile time.

Practical examples

Interaction with COM types

Using dynamic is especially useful when working with COM libraries where types are not known at compile time:

dynamic excel = Activator.CreateInstance(Type.GetTypeFromProgID("Excel.Application"));
excel.Visible = true;
excel.Workbooks.Add();
excel.Cells[1, 1].Value = "Hello World";

Implicit and explicit conversion

dynamic variables can be converted implicitly or explicitly to other types:

dynamic value = "123";
int number = int.Parse(value); // Explicit conversion

value = 456;
int anotherNumber = value; // Implicit conversion

Practical example: processing JSON data

A common practical case for dynamic is processing JSON data:

using System;
using System.Dynamic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;

class Program
{
    static async Task Main(string[] args)
    {
        string url = "https://api.example.com/data";
        using HttpClient client = new HttpClient();
        string jsonData = await client.GetStringAsync(url);
        
        dynamic data = JsonConvert.DeserializeObject<ExpandoObject>(jsonData);
        Console.WriteLine(data.Name);
        Console.WriteLine(data.Age);
    }
}

In this example, dynamic is used to deserialize JSON data into a dynamic object, allowing direct access to properties without the need to previously define a data class.