Why if i dont convert a integer to string the output will be coming?
-
Hi, As i have a small doubt that why if i don't convert a integer to a string using ToString() method then also the output is coming. can any one try this. Example Code: int year = 1998; string message = "Sandy was born in " + year.ToString();//Here if i remove the ToString() //also the code working properly System.Console.WriteLine(message); System.Console.ReadLine();
-
Hi, As i have a small doubt that why if i don't convert a integer to a string using ToString() method then also the output is coming. can any one try this. Example Code: int year = 1998; string message = "Sandy was born in " + year.ToString();//Here if i remove the ToString() //also the code working properly System.Console.WriteLine(message); System.Console.ReadLine();
It still works because, the '+' operator has been overloaded to take various different combinations of types. In this case, the + operator is taking a string an an integer so is calling its overload for 2 objects:
String.Concat(Object, Object)
The integer gets boxed as an object. The Concat() method obviously then calls ToString() on the objects, concatenates the strings and returns the result. Which is then assigned to the 'message' variable. You can confirm this by using ILDasm to look at the compiled IL. (Because there is boxing occurring, this is not as efficient as calling ToString() on the integer yourself, although for most cases it won't actually make any difference)Simon
-
It still works because, the '+' operator has been overloaded to take various different combinations of types. In this case, the + operator is taking a string an an integer so is calling its overload for 2 objects:
String.Concat(Object, Object)
The integer gets boxed as an object. The Concat() method obviously then calls ToString() on the objects, concatenates the strings and returns the result. Which is then assigned to the 'message' variable. You can confirm this by using ILDasm to look at the compiled IL. (Because there is boxing occurring, this is not as efficient as calling ToString() on the integer yourself, although for most cases it won't actually make any difference)Simon
-
Thanks for your response. But actually i did this small program using visual studio 2005. In that how can i use the ILDASM tool. It can be used only when i work with command prompt know. Can you please suggest me....
-
Thanks for your response. But actually i did this small program using visual studio 2005. In that how can i use the ILDASM tool. It can be used only when i work with command prompt know. Can you please suggest me....
Open up a visual studio command prompt (in the start menu, under visual studio tools). Type the command ildasm Click File->Open and select your program. This will show you a tree of the various classes and namespaces within your program. You will be able to browse to the method you are interested in and view the IL for that method. It's quite an advanced tool, and much use if you are just starting out with .net. I used it to verify what method the '+' overload was calling.
Simon
-
Thanks for your response. But actually i did this small program using visual studio 2005. In that how can i use the ILDASM tool. It can be used only when i work with command prompt know. Can you please suggest me....
Instead of using ILDASM directly I recommend using Reflector instead and having a GUI version. http://www.red-gate.com/products/reflector/[^]
Today's lesson is brought to you by the word "niggardly". Remember kids, don't attribute to racism what can be explained by Scandinavian language roots. -- Robert Royall