Interviews - C# Corner

Pramod Thakur
What is the output of following. var a = 5; a = a+"abc"; dynamic b = 5; b = b+"abc"; ?
By Pramod Thakur in C# onJan 16 2016
  • vipin kv
    Mar, 2016 10

    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

    • 5
  • Pankaj  Kumar Choudhary
    Feb, 2016 9

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

    • 3
  • Pramod Thakur
    Jan, 2016 16

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

    • 2
  • SAIF RASHID
    Jan, 2018 24

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

    • 1
  • Kml Surani
    Sep, 2016 6

    a=5abc b=5abc

    • 1
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 1
  • Barkha Gupta
    Jan, 2016 22

    it may return, 5+abc.

    • 1
  • Anil Kumar Murmu
    Jan, 2016 21

    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.

    • 1
  • krishan kumar  singh
    Oct, 2017 11

    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

    • 0
  • Muneer Leo
    Jan, 2017 10

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

    • 0
  • Mohan K
    Nov, 2016 30

    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.

    • 0
  • manisha verma
    Nov, 2016 14

    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.

    • 0
  • manisha verma
    Nov, 2016 14

    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.

    • 0
  • manisha verma
    Nov, 2016 14

    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.

    • 0
  • Naveen Bisht
    Jul, 2016 18

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

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    fd

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    fsdsf

    • 0
  • Suresh Molabanti
    Jun, 2016 15

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

    • 0
  • Suresh Molabanti
    Jun, 2016 15

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

    • 0
  • Suresh Molabanti
    Jun, 2016 15

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

    • 0
  • Suresh Molabanti
    Jun, 2016 15

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

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Suresh Molabanti
    Jun, 2016 15

    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.

    • 0
  • Chetan Raiyani
    Jun, 2016 8

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

    • 0
  • Bhuvanesh Mohankumar
    May, 2016 29

    var a = 5; a = a+"abc" - Error [var - Compile Time assignment] dynamic b = 5; b = b+"abc" - 5abc [Dynamic is Run Time]

    • 0
  • Vivek Kumar
    Apr, 2016 17

    1) Error 2) 5abc

    • 0
  • Keerthi Venkatesan
    Apr, 2016 11

    output would be 5abc

    • 0
  • Amatya Gupta
    Mar, 2016 30

    5abc will be the output

    • 0
  • Sreekanth Reddy
    Mar, 2016 15

    1)Error 2)5abc

    • 0
  • Rajeev Punhani
    Jan, 2016 27

    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.

    • 0
  • Rajeev Punhani
    Jan, 2016 27

    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.

    • 0
  • Sujeet Suman
    Jan, 2016 27

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

    • 0
  • Afzaal Ahmad Zeeshan
    Jan, 2016 25

    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.

    • 0
  • Aneez A
    Jan, 2016 23

    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.

    • 0

Most Popular Companies

Most Popular Job Functions

MOST LIKED QUESTIONS