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. Argument-substitution vs Concatenation

Argument-substitution vs Concatenation

Scheduled Pinned Locked Moved C#
debuggingquestioncsharpjavahtml
6 Posts 6 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.
  • A Offline
    A Offline
    Alvaro Mendez
    wrote on last edited by
    #1

    When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

    int a = 17;
    string b = "something";

    Console.WriteLine("a = {0} and b = {1}", a, b);

    When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

    Console.WriteLine("a = " + a + " and b = " + b);

    I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


    When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

    A J P A L 5 Replies Last reply
    0
    • A Alvaro Mendez

      When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

      int a = 17;
      string b = "something";

      Console.WriteLine("a = {0} and b = {1}", a, b);

      When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

      Console.WriteLine("a = " + a + " and b = " + b);

      I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


      When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

      A Offline
      A Offline
      Alex Korchemniy
      wrote on last edited by
      #2

      I usually use the second one :) Now a days... computers are so fast it doesn't matter how you do it, as long as it works. That's why I use the easier way. But sometimes it helps to know how to use the first one, because in the first method it is easier to read the code, and modify it later.

      1 Reply Last reply
      0
      • A Alvaro Mendez

        When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

        int a = 17;
        string b = "something";

        Console.WriteLine("a = {0} and b = {1}", a, b);

        When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

        Console.WriteLine("a = " + a + " and b = " + b);

        I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


        When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

        J Offline
        J Offline
        James T Johnson
        wrote on last edited by
        #3

        Alvaro Mendez wrote: I'd like to get some opinions on the pros and cons of each approach. By using the argument substitution you can add additional formatting to the WriteLine call (such as pad numbers or strings) which you may or may not be able to do the other way, depending on how the ToString method is written for each variable's type. Another advantage is if you are localizing your application you only have one string to replace instead of 2 for your example. As you point out the con is that you aren't placing the variable right there so you need to reference back and forth to get the whole picture. Alvaro Mendez wrote: as Debug.WriteLine and Trace.WriteLine A shame too, I find myself having to do: string.Format inside of the WriteLine method :( James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

        1 Reply Last reply
        0
        • A Alvaro Mendez

          When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

          int a = 17;
          string b = "something";

          Console.WriteLine("a = {0} and b = {1}", a, b);

          When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

          Console.WriteLine("a = " + a + " and b = " + b);

          I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


          When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

          P Offline
          P Offline
          Paul Ingles
          wrote on last edited by
          #4

          I prefer the first method since it makes the code far more readable. I'm not too sure as to whether one is more efficient than the other, it'd be interesting to see though. I tend to use String.Format("{0}: {1}") etc. when using it with a method that doesn't directly support it. -- Paul "Put the key of despair into the lock of apathy. Turn the knob of mediocrity slowly and open the gates of despondency - welcome to a day in the average office." - David Brent, from "The Office" MS Messenger: paul@oobaloo.co.uk

          1 Reply Last reply
          0
          • A Alvaro Mendez

            When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

            int a = 17;
            string b = "something";

            Console.WriteLine("a = {0} and b = {1}", a, b);

            When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

            Console.WriteLine("a = " + a + " and b = " + b);

            I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


            When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

            A Offline
            A Offline
            Adam Turner
            wrote on last edited by
            #5

            The first way (using String.Format essentially) is a little more useful i think under a scenario like the following

            Console.WriteLine("a = {0} and a = {0} and b = {1}", a, b);

            vs

            Console.WriteLine("a = " + a + " and a = " + a + " and b = " + b);

            1 Reply Last reply
            0
            • A Alvaro Mendez

              When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside Console.WriteLine, similar to printf in C:

              int a = 17;
              string b = "something";

              Console.WriteLine("a = {0} and b = {1}", a, b);

              When I finally got around to writing some code, I decided to stick with the Java approach of simply contatenating everything, which to me seems easier to read and less error prone:

              Console.WriteLine("a = " + a + " and b = " + b);

              I'd like to get some opinions on the pros and cons of each approach. Why does Microsoft seem to promote the first technique? The thing is, you look at other WriteLines, such as Debug.WriteLine and Trace.WriteLine and they don't take a variable number of arguments, so you can't use the argument-substitution technique there. :confused: Thanks, Alvaro


              When birds fly in the right formation, they need only exert half the effort. Even in nature, teamwork results in collective laziness. -- despair.com

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              Thought I'll add me bit as well. I like former approach more, because you can do this:

              Console.Write("Matches for '{1,1}': {0,-10}\t{2,10:f3}ms\t", count, b, Avg(contains));

              :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

              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