Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
using System;
class two
{
   public static void Main()
   {
       double x=3.4;

           double y= 7.0;
       double z;
       z=( x+y);
       Console.WriteLine ("x="+ x,"y=" +y, "z= " + z);
   }
}
Posted
Updated 4-Mar-11 5:24am
v2

Or:

C#
Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);
 
Share this answer
 
Comments
Toli Cuturicu 4-Mar-11 12:09pm    
Way better using string formatting!
R. Giskard Reventlov 4-Mar-11 12:36pm    
Good call: I was bit too quick with my response (even though, technically, it's fine, Henry's is better).
Toli Cuturicu 4-Mar-11 12:49pm    
You got my 4 anyway.
Sergey Alexandrovich Kryukov 4-Mar-11 15:24pm    
Yes of course, my 5, and "+" would be not good enough.
--SA
Sergey Alexandrovich Kryukov 4-Mar-11 15:33pm    
I also decided to add detailed explanation of what's wrong with concatenation, please see.
--SA
Do you mean something like:

Console.WriteLine ("x=" + x + " y=" + y + " z=" + z);
 
Share this answer
 
Comments
rickysharp 4-Mar-11 11:28am    
thank you
The reason to avoid string concatenation operator is this: strings are immutable.
Another reason is the code is less readable is used like in the Question.

So, the problem with immutable strings is this. What happens here:

C#
string result = string.Empty;
int count = 100;
for (int index = 0; index < count; index++)
    result += CalculateNext(index); //some function returning string


The problem here is at every plus string is not appended. Instead, new object is created and old string value (ever increasing) will be copied and assigned to the same variable result.

This is slow, so System.Text.StringBuilder should be used instead. If used sparingly for ad-hoc purposes like in the present Question, concatenation is not a problem, but for readability string.Format is the best; it also has better performance.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900