|
-
Feb 24th, 2005, 05:51 AM
#1
Thread Starter
Fanatic Member
Sting formating and Concatenation
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Feb 24th, 2005, 06:08 AM
#2
<?="Moderator"?>
Re: Sting formating and Concatenation
Try
Code:
int age=28;
string name="Bombdrop";
Console.WriteLine( new String.Format("hello {0} you are {1} years old",name,age.ToString()));
-
Feb 24th, 2005, 06:23 AM
#3
Thread Starter
Fanatic Member
Re: Sting formating and Concatenation
Thanks for the reply John but why is that the best option?
Useful Links
.Net
#Develop, GhostDoc, CodeKeep , be.PINVOKE, Good Code Snippet Site
Krypton Toolkit, XPCC / XP Common Controls, QSS Windows Forms Components
VB.COM
VB.Classic Help File, MB Controls, MZTools, ADO Stored Procedure Generator add-in,
-
Feb 24th, 2005, 07:52 AM
#4
Re: Sting formating and Concatenation
It probably does not make much difference which method you use, but the one below makes it easier to write the string without having to worry about opening/ closing quotes and sticking in plus signs all over the place.
Code:
Console.WriteLine("hello {0} you are {1} years old",name,age.ToString());
-
Feb 24th, 2005, 12:31 PM
#5
Junior Member
Re: Sting formating and Concatenation
Code:
Console.WriteLine("hello {0} you are {1} years old",name,age.ToString());
That's the most easily readable and probably a hair faster. I'd use that.
Dan
-
Feb 24th, 2005, 12:47 PM
#6
Junior Member
Re: Sting formating and Concatenation
I made a little program that tested the two methods and they came out nearly the same, to the millisecond. The {param} method was on average about eight ms faster at repeating the command 1000 times. Hardly a difference I'd consider.
Dan
-
Feb 25th, 2005, 05:23 AM
#7
Re: Sting formating and Concatenation
This one:
Code:
Console.WriteLine("hello {0} you are {1} years old",name,age.ToString());
Uses exactly the same semantics as String.Format(), so there is absolutely no benefit in nesting String.Format inside the writeline call. Its an uneccessary step.
It is better to use the above method rather than using a lot of "+" operators, mainly because its easier to read and also because the object code will be a tiny bit smaller and will a tiny bit faster.
I don't live here any more.
-
Mar 8th, 2005, 01:27 PM
#8
Frenzied Member
Re: Sting formating and Concatenation
I agree with Wossname, but the English major in me would make the following addition:
Code:
Console.WriteLine("Hello {0}. You are {1} year{2} old.",
name, age.ToString(), age == 1 ? "" : "s");
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|