Difference in functionality when displaying text?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Difference in functionality when displaying text?

  1. #1
    Join Date
    Apr 2014
    Posts
    25

    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?

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    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.

  3. #3
    Join Date
    Apr 2014
    Posts
    25

    Re: Difference in functionality when displaying text?

    Quote Originally Posted by Arjay View Post
    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    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...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    Re: Difference in functionality when displaying text?

    Quote Originally Posted by dglienna View Post
    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).

  6. #6
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    83

    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

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    Re: Difference in functionality when displaying text?

    Quote Originally Posted by Arjay View Post
    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
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Apr 2014
    Location
    in northeast ohio
    Posts
    83

    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.

  9. #9
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,984

    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.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,358

    Re: Difference in functionality when displaying text?

    Quote Originally Posted by dglienna View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center