Wednesday, May 12, 2010

.NET 4.0 "dynamic" keyword versus, "var" keyword - anonymous types

The "dynamic" keyword in .NET 4.0.

What is really cool about this keyword is that the actual type attached to your variables is applied at run-time without needing to worry about properties, methods, etc.  Very cool.  Below will be an explanation of this in a little more detail.


The "var" keyword
First you may think that this is just another way to implement the "var" keyword, but this is not true.  The "var" keyword is very different then the "dynamic" keyword.  The "var" keyword does not have the ability to be passed as a parameter to a method or as a return type.

Now one could argue that you can do this by boxing or why you would want to do this in the first place, but this is purely just to show the differences.  In addition, the "var" keyword is strictly typed so when the compiler reaches this line of code it will perform the proper type casting for you and determine if the expression is correct.

For example the following line of code will show a compile error as highlighted in red:















Because the compiler is strictly typing this anonymous type it determines that the method does not exist and causes an error when trying to compile.  Now you can get around this by using reflection or an object, but for this example it is just to show the differences, not so much how to make another way work or not work.

Now let's take a look at the "dynamic" keyword. 

For this example we will take the same example as above and see if this compiles using a dynamic anonymous type.  Furthermore, we will even call a method that we know does not exist in the class.















As you can see with this example the compiler does not check this anonymous type at all or the properties or methods to see if they exist.  It will however dynamically type cast this at run-time.

Summary
In summary, the "var" keyword is an anonymous type and will always be derived from the base object class directly.  Whereas, the "dynamic" type will be handled at run-time allowing for more flexibility at the cost of intellisense.

With all that said, this could be looked at as very dangerous coding.  However, when using objects that are dynamic this could be very handy if used properly.  I could think of a few areas to use this, such as with Web Services where you do not have the WSDL yet, but would like to complete the coding, or legacy COM objects where the properties are dynamic themselves.

I plan to perform some benchmark tests to see if using this has performance impacts and will update my blog when finished.

All comments are welcome.

3 comments:

KenL said...

Two questions - one will this work;

public class test
{
public int id { get; set; }
public string t { get; set; }
public XmlDocument doc { get; set; }
}

public class example
{
public void Main()
{
dynamic t1 = MyList();
t1.SomeDLL();
}

public List MyList()
{
List list = new List();
test t = new test();
t.id = 1;
t.t = string.Empty;
t.doc = new XmlDocument();
list.Add(t);
return list;
}

public virtual override SomeDLL()
{
return;
}
}

and question two does the dynamic varialbe get passed into the method it is calling? In this example does t1 get passed to SomeDLL?

Sean Iannuzzi said...

One advantage of using the dynamic keyword is the ability to pass that into a method. You can define a parameter such as this:

public void myTest(dynamic t2)
{
...some code
}

You cannot do this with a var.

Anonymous said...

However, the patients may feel more comfortable telling them their feelings, because they
feel as if they won't be judged. ll understand what you have to do in order to solve your basic problems. A recovering therapist would be more capable of role modeling how to stay clean and sober.
Feel free to surf my site ... post pardum depression Medications