Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here.

There are two things, static type and strong type checking, with static type checking the compiler will produce an error for any call that fails to pass a method argument of the appropriate type, likewise, you should expect a compiler error if you attempt to call a missing method on a type instance. On the other hand Dynamic type checking contradicts the idea that the type of a variable has to be statically determined at compile time, the difference with dynamic type checking is that the check occurs when the program executes rather than when it compiles.

Dynamic type is introduced with C# 4 (Visual Studio 2010), dynamic is a type. It has a very special meaning, but it’s definitely a type and it’s important to treat it as such. You can indicate dynamic as the type of a variable you declare, the type of items in a collection or the return value of a method. You can also use dynamic as the type of a method parameter. Conversely, you can’t use dynamic with the typeof operator and you can’t use it as the base type of a class.

A major difference between the two keywords is that var can only appear within a local variable declaration. You can’t use var to define a property on a class, nor can you use it to specify the return value or a parameter of a function.

Let’s see some examples:

var str = "some string";

string str = " some string ";

In this case both are identical; in both cases str knows that str.Length means the string.Length property.

Now let’s check the same with dynamic type

dynamic str = "some string ";

In this case str is dynamic type and does not know anything about string.Length property, because it does not know anything about str at compile time.

Let’s try this

dynamic str = "some string ";

If you will try to compile, it will compile successfully but at run time it will give error.

Nathan Armour
  • c#
  • oops
By Nathan Armour On 26 Mar, 13  Viewed: 3,223

Other blogs you may like

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden   On 01 Jun 2013  Viewed: 4,571

How to create windows service in C# visual studio 2010

Creating windows service in C# is really easy but when we need to create first time we face many problems so in this post we will try to learn everything about window service. In this article we will create a windows service which will run after a particular interval, and time should be... By Ali Adravi   On 30 Apr 2013  Viewed: 8,660

Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first... By Ali Adravi   On 29 Mar 2013  Viewed: 4,994

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,623

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 4,587