Is there any difference between the use of {0}
and +
as they both are doing the same work of printing the length on the screen:
Console.WriteLine("Length={0}", length);
Console.WriteLine("Length=" + length);
In mathematics +0 is a value just a little greater than 0 . Also, -0 is a value just a little lower than 0 .
Here is how Wikipedia explains signed zeroes: “Signed zero is zero with an associated sign. In ordinary arithmetic, the number 0 does not have a sign, so that −0, +0 and 0 are identical.
Both are correct. But, zero is more formal than oh. Native speakers, both Americans and Brits, tend to use either of the forms. Limit the use of oh colloquially.
Actually, zero is neither a negative or a positive number. The whole idea of positive and negative is defined in terms of zero. Negative numbers are numbers that are smaller than zero, and positive numbers are numbers that are bigger than zero.
In your trivial example there's no difference. But there are very good reasons to prefer the formatted ({0}
) option: it makes localization of international software much, much easier, and it makes editing your existing strings by third parties much easier.
Imagine for example you're writing a compiler that produces this error message:
"Cannot implicitly convert type 'int' to 'short'"
Do you really want to write the code
Console.WriteLine("Cannot implicitly convert type '" + sourceType + "' to '" + targetType + "'");
? Good heavens no. You want to put this string into a resource:
"Cannot implicitly convert type '{0}' to '{1}'"
and then write
Console.WriteLine(FormatError(Resources.NoImplicitConversion, sourceType, targetType));
Because then you have the freedom to decide that you want to change that to:
"Cannot implicitly convert from an expression of type '{0}' to the type '{1}'"
Or
"Conversion to '{1}' is not legal with a source expression of type '{0}'"
These choices can be made later, by English majors, without requiring changes to the code.
You can also translate those resources into other languages, again without changing the code.
Start always using formatting strings now; when you need to write localizable software that uses string resources properly, you'll already be in the habit.
The second line will create a string and print the string out. The first line will use composite formatting, like string.Format.
Here are some good reasons to use composite formatting.
There is a difference.
ex:
Console.WriteLine("the length is {0} which is the length", length);
Console.WriteLine("the length is "+length+" which is the length");
+
concatenates two strings, {0}
is a placeholder for a string to be inserted.
{n}
is a placeholder which can be used with multiple options. where n is a number
In your example it would make a difference and the end result would be same that is concatenation of two string. However in something like
var firstName = "babba";
var lastName ="abba";
var dataOfBirth = new Date();
Console
.Write(" Person First Name : {0} | Last Name {1} }| Last Login : {2:d/M/yyyy HH:mm:ss}",
firstName,
secondName,
dateOfBirth);
it provides a easy to read interface with easy formatting
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With