Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that you have a misunderstanding of what <code>dynamic</code> means. Essentially, when you tell the compiler that an object's type is <code>dynamic</code>, you "promise" that the object <strong>at runtime</strong> will support whatever methods or properties that you invoke, in exchange for the compiler not complaining <strong>at compile time</strong>. You also promise that you will face the consequences if you break your promise.</p> <p>When you say that an object is <code>dynamic</code>, the compiler cannot make assumptions about the type, so it uses <code>object</code>, knowing that anything can be stored as <code>object</code>. When you make an <code>IEnumerable&lt;dynamic&gt;</code>, it becomes <code>IEnumerable&lt;object&gt;</code>, with one significant difference: you can call any method on its elements, and the compiler will not say a word:</p> <pre><code>IEnumerable&lt;SomeType&gt; original = ... foreach (dynamic x in original.AsDynamic()) { // Using your method Console.WriteLine(x.SomeUnsupportedMethod()); // The compiler is silent! } </code></pre> <p>Since <code>original.AsDynamic()</code> gives a sequence of <code>dynamic</code> objects, the compiler does not complain about your call to <code>SomeUnsupportedMethod</code>. If the method is indeed not supported at runtime, the program will crash; if the method is actually supported by elements of <code>SomeType</code>, there would be no crash, and the method will be invoked.</p> <p>That's all the <code>dynamic</code> will do for you; statically, the "placeholder" will remain <code>object</code>, and <code>typeof</code> will tell you as much. But the exact capabilities of the object (its methods and properties) will not be examined until runtime.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload