4

...compared to plainly returning an object. The magic starts when you assign an object to a dynamic declared variable, so what does returning a dynamic make a difference?

So what is the difference between:

static object CreateMagicList()
{
    return new List<string>();
}

and

static dynamic CreateMagicList()
{
    return new List<string>();
}

They both seem to work exactly the same, in example:

dynamic list = CreateMagicList();
list.Add( "lolcat" );

Note that this is not a practical question. I'm interested in the why part :)

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111
  • See : http://stackoverflow.com/questions/253460/what-is-the-practical-use-of-dynamic-variable-in-c-sharp-4-0 – Bruno May 25 '13 at 00:55
  • Also, you might find this post on MSDN worth a read: http://blogs.msdn.com/b/csharpfaq/archive/2010/01/25/what-is-the-difference-between-dynamic-and-object-keywords.aspx – Justin Helgerson May 25 '13 at 00:56
  • I know the use of a dynamic, read the question please :) – Dirk Boer May 25 '13 at 01:00
  • What about the obvious: CreateMagicList().Add("lolcat"); won't compile if it returns object. – dlev May 25 '13 at 01:04
  • @user2246674 well sure, but my point is declaring a method to return dynamic means expressions involving just the method are treated as dynamic without the assignment at all. – dlev May 25 '13 at 01:29

3 Answers3

2

My best guess is that you are allowed to return dynamic so that you could do this:

private static dynamic Get() {
    return new {X=5};
}
public static void Main() {
    var v = Get();
    Console.WriteLine(v.X);
}

If you could declare Get only as object Get(), then your callers would be forced to replace var with dynamic: otherwise, the code would not compile.

Same goes for a use case without var:

public static void Main() {
    Console.WriteLine(Get().X);
}

without dynamic return type you would have to do an intermediate assignment, or use a cast to dynamic.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • @user2246674 What member variable? No member variables are involved here, only a local variable `v`. – Sergey Kalinichenko May 25 '13 at 01:08
  • @user2246674 I cannot think of another thing that you wouldn't be able to do without allowing a `dynamic` return type. I don't think the compiler designers thought specifically about this use case - they allowed it because they could do it, essentially, for free. It was probably cheaper to allow it than to introduce a special case to prohibit use of `dynamic` as method return type. – Sergey Kalinichenko May 25 '13 at 01:14
  • That sounds like a fair explanation. It also leads to `Get().X` usage, without an intermediate variable at all, which I think would drive home the difference more. – user2246674 May 25 '13 at 01:15
1

I think it a point of understanding specific situation in your example it might not make a big difference but it important to consider that dynamic get created at a runtime so you do not have to unbox anything dynamic just take a form of the return type for instance what if instead of doing this you were required not to bind to dynamic but to list

static dynamic CreateMagicList()
{
    return new List<string>();
}

List<string> list = CreateMagicList();
list.Add( "lolcat" );

this would work fine since in runtime you bind to same property

but this

static object CreateMagicList()
{
    return new List<string>();
}

 List<string> list = CreateMagicList();
    list.Add( "lolcat" );

will give you an error since you have to unbox it

COLD TOLD
  • 13,513
  • 3
  • 35
  • 52
  • In the example, `dynamic list = CreateMagicList9); list.Add( "lolcat" );` is given - what is the point of having a dynamic return-type vs. having an object return-type and assigning it to a dynamic-typed variable before use? – user2246674 May 25 '13 at 01:12
  • as i said it a choice of specific situation – COLD TOLD May 25 '13 at 01:20
0

You can return a dynamic, so that in the future, you could actually return a dynamic. With MissingMethodInvoke and MissingPropertyInvoke overloaded. Dynamics are more than just objects.

Bill Gregg
  • 7,067
  • 2
  • 22
  • 39