Can method parameters be dynamic in C#

Yes, you can absolutely do that. For the purposes of static overload resolution, it’s treated as an object parameter (and called statically). What you do within the method will then be dynamic. For example:

using System;

class Program
{
    static void Foo(dynamic duck)
    {
        duck.Quack(); // Called dynamically
    }

    static void Foo(Guid ignored)
    {
    }

    static void Main()
    {
        // Calls Foo(dynamic) statically
        Foo("hello");
    }
}

The “dynamic is like object” nature means you can’t have one overload with just an object parameter and one with just a dynamic parameter.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)