Why does a method returning a type result in implicit typing of a dynamic one?

In the following code snippet, why is an implicitly typed variable defined as dynamic

instead of the method's return method FluentClass

?

public static class DynamicTest
{
    public class FluentClass
    {
        public FluentClass SomeMethod(dynamic arg)
        {
            return this;
        }
    }
    public static void Main()
    {
        dynamic data = new { Data = 1 };

        var fluentClass = new FluentClass();
        // fluentClass variable is typed FluentClass
        var methodResult = fluentClass.SomeMethod(data);
        // methodResult variable is typed dynamic
    }
}

      

+3


source to share


1 answer


Why does a method returning a type result in implicit typing of a dynamic one?

Because this is the best the compiler can do given the information it has.

This methodResult

is dynamic

because the entire expression used for initialization is dynamic

. And this is the case because it data

is dynamic.

When you use dynamic

, you are telling the compiler not to resolve types at compile time. Instead, they should be resolved according to normal compiler rules, but at runtime.

A variable fluentClass

may contain some implementation fluentClass

that contains an overload that matches the runtime type of the argument data

. In this case, another implementation can be called SomeMethod()

that returns a different type.

You told the compiler to defer type resolution until runtime so that it can't make things fall back into a strongly typed context unless you explicitly state what those things are. In your example, this cannot, so the type remains dynamic

.



Note that you might think that the compiler is identifying the one overload you provided based on its parameter type dynamic

. But that's not how it works dynamic

. The parameter dynamic

only affects the implementation of the method, that is, the code in its body. As far as calling a method is concerned, the parameter is essentially object

. It's just that when a parameter value is used in the body of a method, it has functions dynamic

.

Another way to think about dynamic

is that it accepts any object as input (for example object

), but then allows any member of that object that you think exists (if it doesn't exist, an exception will be thrown at runtime). Usage dynamic

overrides the downstream compiler logic, that is to output any uses of the variable dynamic

, but does not affect the input, that is, the assignment of that variable.

Note also that even if the method call is completely unambiguous, like a static

, where there is only one method with that name, you will still get the result dynamic

. Once you start using it dynamic

, it will stick with you until you provide an explicit cast to revert to a known type.


Related reading:

Very similar to your question, if not duplicates:
Why is a method invocation expression dynamic, even though there is only one possible return type?
Why does this method keep returning dynamic despite the return type in the signature?
Why doesn't this string return string.Format () but dynamically?

More general discussion dynamic

:
What is a "dynamic" type in C # 4.0 used for?
C # 4: a real-life example of dynamic types

+2


source







All Articles