It is dynamic, just like Object

In the book "CLR via C #" it was mentioned that the dynamic keyword corresponds to the FCL type - System.Object. please clarify this.

+3
source share
2 answers

This is not the same from the point of view of C # in general ... but in compiled code, a variable declared as a type dynamicusually (possibly always) corresponds to a CLR field or a local type variable object.

The C # compiler is responsible for ensuring that any source code using this value applies dynamic behavior to it. objectis just a compiler used for storage. It will also apply the attribute [Dynamic]where necessary, so that other code knows that it is being processed dynamically.

, :

public class Foo
{
    public dynamic someField;
}

, , :

public class Foo
{
    [Dynamic]
    public object someField;
}

, :

Foo foo = new Foo();
foo.someField = "hello";
Console.WriteLine(foo.someField.Length);

, , foo.someField , Length .

+5

MSDN:

- , dynamic . , .

:

. , dynamic, . , . object. dynamic , .

( )

dynamic , object (, , ), .

+2

Source: https://habr.com/ru/post/1785394/


All Articles