Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Why if i dont convert a integer to string the output will be coming?

Why if i dont convert a integer to string the output will be coming?

Scheduled Pinned Locked Moved C#
tutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Samiullah
    wrote on last edited by
    #1

    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();

    S 1 Reply Last reply
    0
    • S Samiullah

      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();

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S Simon P Stevens

        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

        S Offline
        S Offline
        Samiullah
        wrote on last edited by
        #3

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

        G S D 3 Replies Last reply
        0
        • S Samiullah

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

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          You can download the .NET Reflector and view the code as IL code.

          Despite everything, the person most likely to be fooling you next is yourself.

          1 Reply Last reply
          0
          • S Samiullah

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

            S Offline
            S Offline
            Simon P Stevens
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • S Samiullah

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

              D Offline
              D Offline
              Dan Neely
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups