39
Reply

What is the output of following. var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc"; ?

Pramod Thakur

Pramod Thakur

Jan 16, 2016
3.7k
1

    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

    vipin kv
    March 10, 2016
    4

    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...

    Pankaj Kumar Choudhary
    February 09, 2016
    3

    var a = 5; a = a+"abc";This code return compile time error.dynamic b = 5; b = b+"abc";This code return 5abc..

    Pramod Thakur
    January 16, 2016
    2

    var a = 5; a = a + "abc"; //Errordynamic b = 5;b = b + "abc";

    SAIF RASHID
    January 24, 2018
    1

    a=5abc b=5abc

    Kml Surani
    September 06, 2016
    1

    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.

    Suresh Molabanti
    June 15, 2016
    1

    it may return, 5+abc.

    Barkha Gupta
    January 22, 2016
    1

    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.

    Anil Kumar Murmu
    January 21, 2016
    1

    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

    krishan kumar singh
    October 11, 2017
    0

    a=a+"abc"; throws a compile time error, cant convert int to string type, b=b+"abc"; returns ==> 5abc;

    Muneer Leo
    January 10, 2017
    0

    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.

    Mohan K
    November 30, 2016
    0

    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.

    manisha verma
    November 14, 2016
    0

    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.

    manisha verma
    November 14, 2016
    0

    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.

    manisha verma
    November 14, 2016
    0

    Cannot implicitly convert type 'string' to 'int' error will display

    Naveen Bisht
    July 18, 2016
    0

    fd

    Suresh Molabanti
    June 15, 2016
    0

    fsdsf

    Suresh Molabanti
    June 15, 2016
    0

    var a = "5";a = Convert.ToInt32(a)+ "abc";

    Suresh Molabanti
    June 15, 2016
    0

    var a = "5";a = Convert.ToInt32(a)+ "abc";

    Suresh Molabanti
    June 15, 2016
    0

    var a = "5";a = Convert.ToInt32(a)+ "abc";

    Suresh Molabanti
    June 15, 2016
    0

    var a = "5";a = Convert.ToInt32(a)+ "abc";

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    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.

    Suresh Molabanti
    June 15, 2016
    0

    a=error (Error Cannot implicitly convert type 'string' to 'int') b=5abc

    Chetan Raiyani
    June 08, 2016
    0

    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

    Vivek Kumar
    April 17, 2016
    0

    output would be 5abc

    Keerthi Venkatesan
    April 11, 2016
    0

    5abc will be the output

    Amatya Gupta
    March 30, 2016
    0

    1)Error 2)5abc

    Srikanth Reddy
    March 15, 2016
    0

    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.

    Rajeev Punhani
    January 27, 2016
    0

    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.

    Rajeev Punhani
    January 27, 2016
    0

    Both case will execute successfully & will give the same output - 5abc.

    Sujeet Suman
    January 27, 2016
    0

    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.

    Afzaal Ahmad Zeeshan
    January 25, 2016
    0

    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.

    Aneez A
    January 23, 2016
    0