Argument-substitution vs Concatenation
-
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
-
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
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.
-
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
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 theWriteLine
method :( James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation -
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
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
-
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
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);
-
When I started learning about C#, I noticed the sample programs would use the funky argument-substitution notation inside
Console.WriteLine
, similar toprintf
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
WriteLine
s, 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
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