displaying variables using + var + instead of {0}?

  • Thread starter Thread starter Robert Blackwell
  • Start date Start date
R

Robert Blackwell

I decided to take a class at the local college here and I'm a little
confused about something.

int age = 5;

Console.WriteLine("You are " + age + " years old");

Previously, I had been reading Learning C# and I learned to type
Console.WriteLine('You are {0} years old",age);

The book I'm using now (required for course) is C# by dissection.

At the end of each project it supposidely "disects" the code etc but I don't
think it's doing a very good job because I feel like if I hadn't already
read 6 chapters of Learning C# I'd be pretty confused.

I didn't see it explain what those plus signs were about.

On another code example it's typed

Console.WriteLine(pennies + " pennies");

Why doesn't it have a plus sign before the variable this time?

Thanks
 
The plus sign is simply an indication that you add up different strings.

string str1 = "You are ";
string str2 = age.ToString(); // using an int in Write(Line) causes it to
be converted to a string
string str3 = " years old";

So the first sentence can now be written like
string str4 = str1 + str2 + str3;
Console.WriteLine(str4);

The second line contains only two strings that are added
string str1 = pennies;
string str2 = " pennies";
Console.WriteLine(str1 + str2);
 
+ add's two strings together.

string szText = "Hello " + "there...";

while the {###} syntax, where ### are numbers from 0 to how many variables
are concerned, takes values that are non-string type, such as int, and
inserts them into your string.

Console.WriteLine ("We are at week {0} of Test number {1}", nWeekNumber,
nTestNumber );

note the following would be invalid:

Console.WriteLine ("We are at week " + nWeekNumber + " of Test number "
+ nTestNumber );

it's invalid because nWeekNumber and nTestNumber cannot be added to a
string, because they are not strings themselves.

hope that helps.
Dan.


"Robert Blackwell" <robbieatwowcentraldotcom> wrote in message
I decided to take a class at the local college here and I'm a little
confused about something.

int age = 5;

Console.WriteLine("You are " + age + " years old");

Previously, I had been reading Learning C# and I learned to type
Console.WriteLine('You are {0} years old",age);

The book I'm using now (required for course) is C# by dissection.

At the end of each project it supposidely "disects" the code etc but I don't
think it's doing a very good job because I feel like if I hadn't already
read 6 chapters of Learning C# I'd be pretty confused.

I didn't see it explain what those plus signs were about.

On another code example it's typed

Console.WriteLine(pennies + " pennies");

Why doesn't it have a plus sign before the variable this time?

Thanks
 
Morten Wennevik said:
The plus sign is simply an indication that you add up different strings.

And by adding, he means + returns a new string that represents the
concatenation of two strings.
 
Console.WriteLine ("We are at week " + nWeekNumber + " of Test number "
+ nTestNumber );

Actually, this would compile just fine, because WriteLine accepts numbers
as parameters.
 
Morten Wennevik said:
Actually, this would compile just fine, because WriteLine accepts numbers
as parameters.

You are correct that Console.WriteLine takes numbers as parameters (it has
about 18 overloads if I counted correctly). Howver, that is not the reason
that the above compiles. You would be calling the Console.WriteLine(string)
since the result of adding strings to numbers is a longer string.

"We are at week" + nWeekNumber
calls an overloaded + operator that takes a string on the left side, an int
on the right side, and returns a string. Then call + with two strings
(result of first + and the string " of Test number "). Then call + of a
string and a number. The end result is one string, that is passed to
Console.Writeline
 
I believe that when you use the + operator in java, it returns a
StringWriter object, which allows a statement like:

String a = "h" + "e" + "l" + "l" + "o";

to execute just as quickly as if we created a StringWriter and kept
appending letters to it. I was hoping that this was the case in C#, so I
ran the following test. Now, it could be that I just don't understand
reflection well enough in c# yet, or that the + operator is somehow
optimized, so i'd really appreciate some feedback. as of now, it seems
that using the "{###}" notation is more optimized because each string is
only copied once.

public static void Main()
{
string a = "Hello ";
string b = "World!";
Console.WriteLine((a + b).GetType());
}

outputs: System.String

cheers,
ben
 
Robert,
As the others have stated the + is the string concatenation operator, under
the covers C# is calling String.Concat.
Console.WriteLine("You are " + age + " years old");

under the covers becomes:
Console.WriteLine(String.Concat("You are ", age, " years old"));

Normally I prefer:
Console.WriteLine('You are {0} years old",age);

As its easier to internationalize, you can replace the "You are {0} years
old" (the format) with a string in a different language (German for
example), because age is a placeholder, the position of age within the
format can move and the Console.WriteLine will still succeed. When
internationalizing you would read the format from a Resource file (.resx or
..resources) using a System.Resources.ResourceManager.

Hope this helps
Jay
 
Ben T. said:
I believe that when you use the + operator in java, it returns a
StringWriter object, which allows a statement like:

String a = "h" + "e" + "l" + "l" + "o";

to execute just as quickly as if we created a StringWriter and kept
appending letters to it.

In fact it executes significantly quicker in both Java and C#, because
both recognise that everything in the above is a constant. The above is
exactly equivalent to:

String a = "hello";

See Jay's reply for other stuff.
 
This makes better sense after you explained it since I'm slightly familiar
with PHP and it uses a . instead of a +

echo "Welcome ".$currentUser."!";

so, depending where the variable is being added, before preiod goes after,
between, on both sides, and after period goes before.


Okay, so if I'm understanding you guys correctly {3} is the old way

and + is the new way?

And I probably shouldn't use {0} anymore? or does it depend...

because I thought I remember that if you type something like this
Console.WriteLine("The current road conditions are {0}", (int) icey);

and that would display icey intead of the variable assigned to icey?

I'm not possitive if I typed that correctly, I'd have to go back to my other
book to make sure.
 
and that would display icey intead of the variable assigned to icey?

I meant it would display icey instead of the value assigned to icey
 
in C#, does it matter if there is a space between the + and the variable?
there's not space between the period in PHP.

or is that just a programmers style preference?
 
Robert,
Okay, so if I'm understanding you guys correctly {3} is the old way
and + is the new way?
No. Consider:

int temp = 100;
int wind = 10;
int rainfall = 0;

string format1 = "Temperature: {0} Wind: {1} Rainfall: {2}";
string format2 = "Wind: {1} Rainfall: {2} Temperature: {0}";
string format3 = "Rainfall: {2} Wind: {1} Temperature: {0} ";

Console.WriteLine(format1, temp, wind, rainfall);
Console.WriteLine(format2, temp, wind, rainfall);
Console.WriteLine(format3, temp, wind, rainfall);

Notice that I use one of three format strings, but the other parameters are
given in a fixed order. Notice that in each format string that I have the
same placeholders {0}, {1}, {2}, but they show up in a different sequence.
If you run the above your output would be:

Temperature: 100 Wind: 10 Rainfall: 0
Wind: 10 Rainfall: 0 Temperature: 100
Rainfall: 0 Wind: 10 Temperature: 100

Now I used English on all three, however consider what happens when format1
is English, format2 = French, format3 = German. & the format string itself
is read from a Resource file which are organized by language.
And I probably shouldn't use {0} anymore? or does it depend...
No you should use {0}! especially if you want to internationalize your app.
because I thought I remember that if you type something like this
Console.WriteLine("The current road conditions are {0}", (int) icey);

int icey = 100;
Console.WriteLine("The current road conditions are {0}", (int) icey);

Prints: "The current road conditions are 100"

Hope this helps
Jay
 
Yeah, I think I got it now...was just taken by surprise since it was new and
all.

Thanks guys.
 
Robert,
The FormatMessage Win32 API works in a similar way, as far as I know its
been around 'for ever'. Only it uses %0, %1, %2 for the place holders.

MFC has a similar function (more than likely uses the above function).

When I worked on As/400s it used &1, &2, &3 for the place holders.

So I'm used to they syntax.

Jay
 
Back
Top