how old is this is Philip and today I'm
going to talk to you about dynamic
dynamic is a new keyword in C sharp for
actually it's a contextual keyword and
if you've watched my previous videos on
c-sharp 5 and the async CTP you've heard
me talk about the contextual keywords
before this is a quote from MSDN which
describes that a contextual keyword is
used to provide a specific meaning in
the code but it's not a reserved word in
c-sharp and for those that have
developed any MVC 3 applications in the
early days when the preview and the
release candidate were out you've seen
the the dynamic keyword used in
interviews and how how this was used to
handle the models I'm going to show you
a demonstration of this in a little bit
here so C sharp is an odd dynamic
language and even though there's a
dynamic keyword you have to know that
this is actually a static type so the
dynamic keyword is actually a static
type so in the end this is just an
object that you use as any other object
right so I'm going to show you a couple
of demos I'm gonna start off by showing
you how they did this in in MVC 3 it
doesn't matter really if you know an MVC
3 or any web development I'm just going
to show you a couple of cool ways that
they made use of the dynamic keyword so
what I'm gonna do here is that I'm gonna
create a page that's just gonna display
some information about a person and so
I'm gonna expect a model a dynamic model
okay so what I'm gonna do here is that
I'm gonna be writing out the person's
name and I expect that the name is
actually a property on my dynamic type
here so I expect that the model passed
to my view here is going to have a
property name
so if I go into my home controller here
and add a new action result and and
create a new expanded object the
expanding object is the object that to
use when you want to create a dynamic
type so what I can do now is that I can
add properties here so for instance I
can add the name property I could add
the age and I could even add a method to
this if I want you
and all this works it even compiles and
this is pretty cool I could call my
person dot my method I could call person
dot name and you see here that I've got
intellisense inside this context but
once that I go into my view again I
won't have this this intellisense
because the type is dynamic so I'm not
gonna get any intelligence so if I just
return the relevant properties here for
my person and go back into here and and
add another property is age old age so
what I have now is that I have a string
and I have an integer and let's see how
this is represented on the webpage
and there it is Philip Ekberg age ten so
this is how we might have seen the
dynamic keyword used in Anna in the
early version of asp.net MVC I won't
argue with you if this is the the the
good way or the bad way to do it
generally you might want to have your
models strongly typed you might have
seen here that I could add a method and
I want to show you an example that I can
actually run and show you how this this
works so I'm gonna go into a console
application here that I just created and
what I'm going to do is that I'm going
to add a some kind of method dynamic X
is equal to new Expando object X dot
some method is equal to you a new action
and this action will just do a
console.writeline hello Phillip and what
I'm going to do now is that I'm gonna
invoke some method and I still get the
intellisense because I'm in the same
context here and if I run this
application here it's gonna say hello
Phillip so what happened here is that it
actually finds some method at runtime
and so if I run this here you see that
it prints out hello Phillip and this
worked what I could even do is that I
can remove the expanded object here and
I could say that dynamic sum method and
just a run some method
if we have this person here and we add a
couple of properties to this Phillip
person dot H is equal to 24 person dot H
is equal to 24 you see that we don't
actually get any compilation error so we
don't get any intellisense errors so if
I print this object here that's all the
right time my name my name is is this
person dot name and the age the age is
that and if I run this here it's gonna
run just fine this is one of the
benefits of using the dynamic keyword
and also one of the pitfalls because you
don't have the intelligence to tell you
when something is wrong and you don't
have any any runtime checks that the
values actually set to something that
you have up here you expect that the
person dot H a reset integer and when
you down here you change that to become
a string instead so what happens if I do
if person dot H is bigger than 20 I do
this hello so if I run this application
here I hopefully I'm gonna get an
exception and I did but if I remove this
two lines here and running the
application again it's gonna say hello
so this has been a brief introduction to
the dynamic keyword and I've shown you
how it's been used and I've shown you
some some of the things that you need to
be aware of I hope you guys enjoy this
thank you for watching