Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2012/5.0 > C# 2012/5.0 General Discussion
| Search | Today's Posts | Mark Forums Read
C# 2012/5.0 General Discussion Discussions about the C# 2012 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2012/5.0 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 23rd, 2012, 08:42 PM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default Why bother with {0}-ish string formatting?

Why bother with Console.Write("{0} ", number); instead of Console.Write(number.ToString());?

Or examples where there are several variables and strings being strung together - surely string concatenation is clearer and faster to write, and if you're doing it multiple times you can consider a StringBuilder.

Thanks for any input...
 
Old December 25th, 2012, 07:33 AM
Registered User
Points: 21, Level: 1
Points: 21, Level: 1 Points: 21, Level: 1 Points: 21, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2012
Location: Austin, Tx.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Interesting question. Since .ToString() also gives you formatting capabilities. My guess is that Console.Write() uses .ToString() under the hood, and is simply for convenience as it mimics the traditional 'C' printf() function.
 
Old December 25th, 2012, 05:00 PM
Registered User
Points: 27, Level: 1
Points: 27, Level: 1 Points: 27, Level: 1 Points: 27, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2009
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
Default

Thanks... I was really just wondering about the use of bracketed numbers to refer to the comma-separated list of variables. I guess that people think it makes it easier to read.
 
Old December 26th, 2012, 06:22 PM
Registered User
Points: 14, Level: 1
Points: 14, Level: 1 Points: 14, Level: 1 Points: 14, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Dec 2012
Location: NYC, NY
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Lightbulb Why the {0} conventions. .

Another advantage to using this would be repeating the arguments like: Console.WriteLine("....{0}...{1}...{0}..{2}...{0}. .{1}", myVar, hisVar, herVar);

Imagine how much more you would have to type otherwise.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Hey I am here again to bother you :) nawar youssef BOOK: Beginning PHP 5.3 0 November 27th, 2011 12:16 AM
VB questions, around pg 185-ish jefals BOOK: Beginning ASP.NET 4 : in C# and VB 1 November 20th, 2010 12:05 PM
String Formatting sumith ASP.NET 2.0 Basics 1 October 3rd, 2007 07:35 AM
String formatting MargateFan XSLT 4 May 5th, 2006 04:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.