Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C#

dynamic — binding operations at runtime

The dynamic type defers operation binding until runtime. It simplifies interaction with dynamic APIs, but missing members or incompatible arguments can cause RuntimeBinderException instead of a compilation error.

Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification

Syntax

dynamic name = expression;

dynamic lets you defer checking and binding of certain operations until runtime. The compiler records information about the operation, and the binder resolves it later using information that includes the actual types of values. The feature dates from C# 4; it is neither new nor a preview feature in .NET 10. The repository setting LangVersion=preview is not required for it.

Unlike dynamic, var infers the initializer's type: in var x = 42, the variable has type int and is statically checked. object also retains static checking of available operations; accessing members of a specific type usually requires a cast or pattern matching. dynamic is represented as object, not as a separate runtime type. Assigning a value to it neither changes that value's type nor adds members to it.

Dynamic binding through DLR uses the Dynamic Language Runtime infrastructure. System.Runtime.CompilerServices.CallSite<T> represents a dynamic call site; T is the delegate type, and cached rules reduce repeated binding. System.Dynamic.IDynamicMetaObjectProvider lets an object supply custom binding behavior through GetMetaObject. An ordinary .NET object does not need to implement this interface to be used through dynamic.

Calls with a dynamic receiver or arguments can select an overload only at runtime. The result of most dynamic operations also has type dynamic; assigning it to, for example, int requires a valid conversion checked at runtime. Failed binding, such as reading a nonexistent property, causes Microsoft.CSharp.RuntimeBinder.RuntimeBinderException. Exceptions thrown by a successfully invoked method do not automatically become this exception.

Use dynamic at integration boundaries with COM or dynamic APIs, and prefer static types when the contract is known. You lose some compiler checking and incur binding overhead. In the example, Length works for string, but after assigning an int, the same property name produces a handled binding failure.

Examples

using System;
using Microsoft.CSharp.RuntimeBinder;

internal static class Program
{
    private static void Main()
    {
        dynamic value = "hello";
        int length = value.Length;
        Console.WriteLine(length);

        value = 42;
        Console.WriteLine(value + 1);

        try
        {
            Console.WriteLine(value.Length);
        }
        catch (RuntimeBinderException)
        {
            Console.WriteLine("RuntimeBinderException");
        }
    }
}

Result

5
43
RuntimeBinderException

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources