-
June 5th, 2014, 07:55 AM
#1
Difference in functionality when displaying text?
It's the little things right now ...
What's the functional difference between these two WriteLine()statements?
Code:
string you = "You", they = "They";
Console.WriteLine("Me " + you + " " + they);
Console.WriteLine("Me {0} {1}", you,they);
Is it just a readability thing?
-
June 5th, 2014, 12:48 PM
#2
Re: Difference in functionality when displaying text?
The second one uses the params[] array built into Console.WriteLine. It's similar to String.Format(...).
IMO, that's the preferred approach for few reasons (these are only a couple of them)
1) Usually your store the formatter (i.e. the "Me {0} {1}" part) as a constant or as a resource string.
Then you can reuse it in multiple places in your code. If you decide to change the format, you only
have to change the constant or resource string.
2) If you make it a resource string, it's localizable.
-
June 5th, 2014, 01:46 PM
#3
Re: Difference in functionality when displaying text?
Originally Posted by Arjay
The second one uses the params[] array built into Console.WriteLine. It's similar to String.Format(...).
IMO, that's the preferred approach for few reasons (these are only a couple of them)
1) Usually your store the formatter (i.e. the "Me {0} {1}" part) as a constant or as a resource string.
Then you can reuse it in multiple places in your code. If you decide to change the format, you only
have to change the constant or resource string.
2) If you make it a resource string, it's localizable.
Yes, that logic is sound. Thank you.
-
June 5th, 2014, 08:43 PM
#4
Re: Difference in functionality when displaying text?
They, isn't a string:
Code:
string you = "You", they = "They";
You is, but They is not a string...
-
June 5th, 2014, 09:41 PM
#5
Re: Difference in functionality when displaying text?
Originally Posted by dglienna
They, isn't a string:
Code:
string you = "You", they = "They";
You is, but They is not a string...
Sure it is. In c# you are allowed multiple variable declarations and assignments (providing they are all the same type).
-
June 5th, 2014, 09:46 PM
#6
Re: Difference in functionality when displaying text?
there may be another difference as well unless this has changed or i am mistaken
feel free to correct me if im wrong. this is based on what i've read but not verify-ed
while practically the same there maybe some functional differences under the hood
as i understand it
string format uses string builder AppendFormat to combine the strings deep down which isn't exactly the same as append
were + does not, its more like a for loop on the strings ,,,
so a lot of people Hate to see this " "+" " as in hate it in anything other then example or test run code
so there might also be a difference on how much garbage is generated and ultimately collected between the two
if im correct the basic answer is no the super technical answer is yes
it seems only the code or il output knows because ive heard
under 4 ""+"" the compiler optimizes it to basically String.Concact which is basically a string builder append,,
here is msdn's page on string formating which is basically the recommended way other then using stringbuilder
http://msdn.microsoft.com/en-us/libr...ormat1_Example
but implementationally it seems opinions abound everywere, ive read and seen a lot of debate on this and what the compiler does
http://geertverhoeven.blogspot.com/2...ut-string.html
depending on how technical you want to go this can get deep
http://stackoverflow.com/questions/3...019525#3019525
http://stackoverflow.com/questions/4...-string-format
http://stackoverflow.com/questions/1...or-concat-in-c
this is another good read
http://csharpindepth.com/Articles/General/strings.aspx
and then there is string interning , this is one for everyone to scratch there head at really deep down
http://broadcast.oreilly.com/2010/08...gintern-m.html
you could spend days researching it and only come out with a headache
here's a nice video on ms channel 9 about string use and format some little tricks and tips and stringbuilder as well
http://channel9.msdn.com/Series/C-Sh...ith-Strings-12
actually there is a whole series here for people who like video's instead of reading
http://channel9.msdn.com/Series/C-Sh...lute-Beginners
Last edited by willmotil; June 5th, 2014 at 11:35 PM.
Reason: thought about it too much
-
June 5th, 2014, 11:02 PM
#7
Re: Difference in functionality when displaying text?
Originally Posted by Arjay
Sure it is. In c# you are allowed multiple variable declarations and assignments (providing they are all the same type).
Not here: http://msdn.microsoft.com/en-us/library/ms228362.aspx
-
June 6th, 2014, 12:25 AM
#8
Re: Difference in functionality when displaying text?
?
Code:
static void Main(string[] args)
{
string A = "a", B = "b";
string formattype = "{0} type {1}";
Console.WriteLine(formattype, "A", A.GetType());
Console.WriteLine(formattype, "B", B.GetType());
Console.WriteLine(formattype, "a", "a".GetType());
Console.WriteLine(formattype, "b", "b".GetType());
Console.ReadLine();
}
output
A type System.String
B type System.String
a type System.String
b type System.String
below i still dont get what your trying to say, please explain
string you = "You", they = "They";
002226D3 mov eax,dword ptr ds:[33821D8h]
002226D9 mov dword ptr [ebp-3Ch],eax
002226DC mov eax,dword ptr ds:[33821DCh]
002226E2 mov dword ptr [ebp-40h],eax
Last edited by willmotil; June 6th, 2014 at 04:46 AM.
-
June 6th, 2014, 01:00 AM
#9
Re: Difference in functionality when displaying text?
OK, if you say so!
How about this?
Code:
using System.IO;
using System;
class Program
{
static void Main()
{
// Read in every line in the file.
String A = "a", B = "b";
string formattype = "{0} type {1}";
Console.WriteLine(formattype, "A", A.GetType());
Console.WriteLine(formattype, "B", B.GetType());
Console.WriteLine(formattype, "a", "a".GetType());
Console.WriteLine(formattype, "z", "Z".GetType());
Console.ReadLine(); }
}
Last edited by dglienna; June 6th, 2014 at 01:04 AM.
-
June 6th, 2014, 03:09 PM
#10
Re: Difference in functionality when displaying text?
Originally Posted by dglienna
I think the docs is out of date.
Not sure when support for multiple declaration and assignment was added, but it definitely is in there (at least for 4.0 and 4.5).
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
|