locked
Difference between 'string' and 'String'

    Question

  • Hi

    I would like to know the difference between 'string' and 'String' in CSharp 2.0. I had thought that 'string' mapped onto the Framework Type 'System.String' but I got a code compile error that I cannot explain. I am using Visual Studio 2005 (but I have also installed SP1 Beta).

    Consider the following two lines of code.
    private static String _uid1 = new String(Guid.NewGuid().ToString());
    private static String _uid2 = new string(Guid.NewGuid().ToString());

    The code for uid1 generates an error but not for uid2. The code for uid11 states the error is because 'System.String' does not have a constructor that takes a parameter of type 'String'. So what is happening so that uid2 does not generate an error? Is there some implicit conversion happening by Visual Studio 2005?


    Monday, October 09, 2006 4:47 AM

Answers

  • Read the class String constructors from the help, then you get answer like: "A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string." In you case ToString() does not return char[], what is expected by the constructor. You probably don't have any exception handlers, so you don't get past the first exception.
    Monday, October 09, 2006 6:09 AM
  • string is an alias for System.String so it should behave exactly the same.

    I actually get an error for both 2 lines in your example and error is correct. The string class does not have a constructor that takes a string as a parameter.

    You don't need it anyway, you can write the following instead.

    private static string _uid1 = Guid.NewGuid().ToString();

    private static string _uid2 = Guid.NewGuid().ToString();

     

    Monday, October 09, 2006 6:21 AM

All replies

  • Read the class String constructors from the help, then you get answer like: "A string is a sequential collection of Unicode characters, typically used to represent text, while a String is a sequential collection of System.Char objects that represents a string." In you case ToString() does not return char[], what is expected by the constructor. You probably don't have any exception handlers, so you don't get past the first exception.
    Monday, October 09, 2006 6:09 AM
  • string is an alias for System.String so it should behave exactly the same.

    I actually get an error for both 2 lines in your example and error is correct. The string class does not have a constructor that takes a string as a parameter.

    You don't need it anyway, you can write the following instead.

    private static string _uid1 = Guid.NewGuid().ToString();

    private static string _uid2 = Guid.NewGuid().ToString();

     

    Monday, October 09, 2006 6:21 AM
  • To confirm what Mike said: String and string are identical.

    However, if you have a local type called String() that may be used instead, and it may be your own local String() that has added a string parameter to the constructor.


    Monday, October 09, 2006 9:23 AM
  • Thanks for your reply.

    I had expected that 'string' was an alias for 'System.String'.  Visual Studio 2005 had not indicated that an error with the dynamic code checker in its editor.  Now that I have built the project again, it shows both lines are in error.

    As for the code, it was taken from an open source project.  You are right that it was badly implemented from the start.
    Wednesday, October 11, 2006 12:05 AM
  •  Matthew Watson wrote:
    To confirm what Mike said: String and string are identical.

    However, if you have a local type called String() that may be used instead, and it may be your own local String() that has added a string parameter to the constructor.




    "String" is from "System.String" (.NET Data Type)
    "string" is from "string" (C# Type, a reserved word in C#)

    Both are identical in some ways. But there are minor differences that people experience between them.

    HTH.
    Wednesday, October 11, 2006 2:02 AM
  •  Leonard Lee wrote:
    Both are identical in some ways. But there are minor differences that people experience between them.

    I don't see how there could be any differences. The two words are literally interchangeable; "string" is a C# alias for the built-in type "String", just like 'long" is an alias for "Int64". If you decompile two assemblies which are identical except that one uses "string" and one uses "String", you will find that there are quite literally exactly identical.

    Wednesday, October 11, 2006 3:32 AM
  • Your original problem was in constructor, what I explained in my first answer.

    >> private static String _uid1 = new String(Guid.NewGuid().ToString());

    So, what are you doing with?

        Guid.NewGuid()

    You get integer as explained by help:

    “A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.”

    Next you use ToString() to convert it to string like you can see in your registry.

    But, class String has no constructor for making other instance of 'itself' ( it has sealed attribute, which prevent inheritance), it need more atomic base type to wrap as is char array. What is class String? I think , that it's more specialiced case of class Char and do it's job with char array.

    After saying that, why you need GUID string? You can keep it in the 128-bit integer:

    Decimal newGuid = Guid.NewGuid();

     

    Wednesday, October 11, 2006 4:21 AM

  • Hi

    My question was not about how the code is used.  The code is from an open source project and I personally would not have written this code this way. 

    My question was why I observed a difference in behavior between using 'string' and 'String' in Visual Studio.NET 2005.  I now know the difference was only due to the dynamic code checker inside Visual Studio 2005 not recognizing the first line as an error until the code is explicitly compiled.  I had not immediately picked up on this as the code had other errors that stopped a clean compile.

    So 'string' and 'String' are no different as I had originally thought.  I should have done a little more investigation before making this post.  However, I did learn why copy constructors are not used in the .NET Framework.

    Sorry to waste your time.
    Wednesday, October 11, 2006 8:39 AM
  • Not a waste, salsa, as long as you learned something :)
    Wednesday, October 11, 2006 3:29 PM