var is statically typed and dynamic is dynamically typed. var needs to initialize at the time of declaration and cannot be changed. Since the compiler has already decided the type var variable we cannot assign other values to var variable it violates the type safety and compile time error will throw.But on other hand we can declare dynamic variables and later we can assign any values to it
First Expression will throw an compile time error related to conversion of string to int and second expression will run successfully and give a result like "5abc" because type for dynamic variable is decided at run time so in this expression b+"abc" = 5+"abc" will execute as concatenation of two string...
var a = 5; a = a+"abc";This code return compile time error.dynamic b = 5; b = b+"abc";This code return 5abc..
var a = 5; a = a + "abc"; //Errordynamic b = 5;b = b + "abc";
a=5abc b=5abc
ar a = 5; a = a+"abc" it gives compilation error can't convert string to int. why because 5 is an integer.int+string is not valid.that's why error is coming.but it is possible var a = "5";a = Convert.ToInt32(a)+ "abc";Console.WriteLine("Enter a number");var b = Console.ReadLine();------->6b = b + "abc";Console.WriteLine(b); Answer is:6abc here,enter dynamic value is treated as string.
it may return, 5+abc.
var type of variables are required to be initialized at the time of declaration otherwise they would encounter the compile time error. However, for dynamic type, it would produce 5abc.
var a = 5;a = a+"abc"; //Compile time error (cannot implicitly convert string to int)dynamic b = 5;b = b+"abc";Console.WriteLine(b);// 5abc
a=a+"abc"; throws a compile time error, cant convert int to string type, b=b+"abc"; returns ==> 5abc;
var: It is a compile time entity dynamic : it is a run time entityvar a = 5; a = a+"abc"; Ans: compile time error, here a is integer datatype and trying to do addition operation with string datatype. so we cannot add integer with stringdynamic b = 5; b = b+"abc"; Ans: at first b is a integer, then to do operation for adding with string, here dynamic will convert b integer data type to string data type then concatenate the both value.
var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc"; In the var a = 5 // assigned integer value 5 to var so data type will be int as var is early bonded so it will decide on compile that what the data type of value is assign. so when a = a+"abc"; will be execute it will give the conversion error.
Cannot implicitly convert type 'string' to 'int' error will display
fd
fsdsf
var a = "5";a = Convert.ToInt32(a)+ "abc";
var a = 5; a = a+"abc" it gives compilation error can't convert string to int. why because 5 is an integer.int+string is not valid.that's why error is coming.Console.WriteLine("Enter a number");var b = Console.ReadLine();------->6b = b + "abc";Console.WriteLine(b); Answer is:6abc here,enter dynamic value is treated as string.
a=error (Error Cannot implicitly convert type 'string' to 'int') b=5abc
var a = 5; a = a+"abc" - Error [var - Compile Time assignment] dynamic b = 5; b = b+"abc" - 5abc [Dynamic is Run Time]
1) Error 2) 5abc
output would be 5abc
5abc will be the output
1)Error 2)5abc
var a=5;a=a+"abc" ; will give compile time error as var is implicitly typed local variable and it takes the datatype of the value that is initialized to it .Now when you try to assign any other type value to it ,it will throw compile time error.In case of dynamic b=5;b=b+"abc"; It will run as dynamic types takes the data type of whatever you assign to it and you can also change its type at runtime.
Both case will execute successfully & will give the same output - 5abc.
In the first case, there would be a compiler error. Because a is of type integer, and thus it won't work when you add integer and string.
var a=a+"abc" will be an error. Will show cannot convert type string to int. But b=b+"abc" will give you an output 5abc.