Welcome to Questpond s YouTube channel. Today we are discussing a very important C#
Interview Question what is the difference between
Var and Dynamic keywords in C#. Before answering this question, would like
to make a very important point. Why on this Questpond s YouTube channel I record interview
questions? Because Working in C# or ASP.NET or Angular is one think but cracking a C#
interview is a different ball game. Definitely, you need to have a Working experience definitely
you should know how to complete a project but at the end of the day what happens you
gets such kind of questions var vs Dynamic and it is very much possible that in your
project your never use the Dynamic keyword. Probably you never know what this is.
From the interviewer s perspective this is important, simple and this has to be answered.
I would suggest go down on the description and the comment section I have lots of videos
on C# interviews ASP.NET and Angular go through them.
Let us try to answer this what is a difference between Var and Dynamic now for some reason
some of developer friends say that Var is Dynamically typed Var Datatypes are detected
on run time. I don t know why they say that must be there are two categories people the
one who thinks that VAR means VARYING and it can VARY It can change it is Dynamic so
they say that Var is Dynamic. The second category people who are coming from visual basic.
In visual basic in a we have this Variant Keyword and this variant Keyword or the Variant
Variable can go from house to a mouse to a lion to a small integer. That is another kind
of people who think that Var is Dynamically typed.
If you are saying in the interview that Var is Dynamically Typed you are sending a very
very bad impression to the interviewer and it is very must possible that your interview
not continue. When you say Var and it is declared a Data Type it is statically Typed.
The Data Type is known during the run time in the run time it self the compiler looks
at the right-hand side for example you can see here in this line number nine we have
var x = csharpinterviewquestions it goes and looks at the Data on the right-hand side and
says what is this Data on the right-hand side? It s a string and he makes this x as a string.
If I change here let say If I put here a number now it will go and say at the right-hand side
it is a number. It has made this X as a Integer, first thing the Var Keyword is statically
typed. As you write the code the compiler looks at the right-hand side and determines
what is the Data Type. Here this X is now a string and if I say x.
all the properties and the method of the Strings of the .NET string is out x.length, x.Clone,
xTrim, and so on Var keyword is statically Typed.
Coming to Dynamic as the words Dynamic this is completely Dynamic.
For example if I move my mouse on this y, there is no Data Type and if I say y. something
he does not give me anything out that is nothing in the intellisense. Here the methods, the
properties will be checked and executed during the run time. Over here if I say y.Length
this one should work. If I run this now because y is pointing towards the String there should
be a Length. On run time it determines that yes this length exists, this property exists,
it invoked it and it gave the length of string. If I make a Mistake here for example if I
say this is small l here it will allow in to build. Build 1 succeeded but during the
run time you will have a problem. When you say y.length; and if you execute this, string
does not contain a definition for length. Remember that Dynamic Keyword executes and
checks during the run time while the Var is completely statically typed it is checked
during the compile time it looks at the Data it the right-hand side and determines what
kind of object or the variable it is. Summarize the answer, Var statically typed
while Dynamic is completely on run time. Hope that this Session was useful. We were trying
to understand what is the difference between Var vs Dynamic. Down below you can see all
our other Interview Questions our 30 C# important interview questions, 25 ASP.NET important
interview questions, 25 Angular Interview Questions and many such Interview Questions
down below. Happy Learning, Happy Job Hunting Thank you
!