strings

This is a discussion on strings within the C# Programming forums, part of the General Programming Boards category; 1) Is there a difference between String and string (or between Object and object)? 2) Why can't you just create ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    strings

    1) Is there a difference between String and string (or between Object and object)?
    2) Why can't you just create an empty string? there is no zero parameter constructor for string...

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    "string" is just an alias for "System.String", just like "int" is an alias for "System.Int32" and "object" for "System.Object". Whichever you use is up to you.

    How about:
    Code:
    string EmptyString1 = "";
    string EmptyString2 = System.String.Empty;
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Ah, ok.

    II want the new keyword so I can instantiate a new string so I can use it outside a function. Like:
    Code:
    void myMyAriAri(object o)
    {
        ((myClass)o).str = new string(String.Empty);
    }
    but that is not right, because string doesn't have such a constructor. I could do new string(new char[0]) or something like that, but again... why not have a standard constructor for a null string.

    Of course, I could just use String.Copy() at one point and not use the new keyword. But, why not have the option?

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    C# is managed and System.String is immutable. Just do "((myClass)o).str = System.String.Empty;".
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    immutable... I wasn't aware of that. So it makes sense now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21