locked
What's the difference between string and String?

    Question

  • What is the difference between string and String?

    Which of them should i normally use?

    Friday, April 14, 2006 5:26 PM

Answers

  • Nothing really, in C# the type keywords actually are synonyms for the types. So int = System.Int32 short = System.Int16 and string = System.String.

    They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.

    Anyway, look at the C# keyword reference and you can find these things out. This was taken from the string keyword reference.

    The string type represents a string of Unicode characters. string is an alias for String in the .NET Framework. Strings are immutable--the contents of a string object cannot be changed after the object is created.

    Friday, April 14, 2006 5:35 PM

All replies

  • Nothing really, in C# the type keywords actually are synonyms for the types. So int = System.Int32 short = System.Int16 and string = System.String.

    They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.

    Anyway, look at the C# keyword reference and you can find these things out. This was taken from the string keyword reference.

    The string type represents a string of Unicode characters. string is an alias for String in the .NET Framework. Strings are immutable--the contents of a string object cannot be changed after the object is created.

    Friday, April 14, 2006 5:35 PM
  • Hi,
    it's an alias, so you use whatever you want. You can do things like:
    string str = new String();

    You have other aliases like:
    int = Int32
    char = Char
    bool = Bool
    etc..
    Friday, April 14, 2006 5:36 PM
  • As far as I know, string is just an alias for System.String, and similar aliases exist for bool, object, int... the only subtle difference is that you can use string without a "using System;" directive, while String requires it (otherwise you should specify System.String in full).

    About which is the best to use, I guess it's a matter of taste. Personally I prefer string, but I it's not a religious issue.

    --mc

    Friday, April 14, 2006 5:43 PM
  • I agree with every one, "string" and "System.String" are the same as it comes to functionality of it, and it's a personal choice.

    But when you use Visual Studio 2003.NET you will notice keywords are highlighted in blue, and class names aren't. I personally prefer such keyword highlighting so I got used to using keywords like string and bool instead of System.String and System.Boolean. Now in Visual Studio 2005, class name are also color coded which is kind of cool.

    Friday, April 14, 2006 6:20 PM
  •  Yogesh Prabhu wrote:

    I agree with every one, "string" and "System.String" are the same as it comes to functionality of it, and it's a personal choice.

    But when you use Visual Studio 2003.NET you will notice keywords are highlighted in blue, and class names aren't. I personally prefer such keyword highlighting so I got used to using keywords like string and bool instead of System.String and System.Boolean. Now in Visual Studio 2005, class name are also color coded which is kind of cool.



    Funny that I liked the String keyword highlight (the light blue/gree) better than string (blue as for all other keywords) but for the other types like bool, char, int, etc. I like the blue one.

    According to this:
    http://blogs.msdn.com/csharpfaq/archive/2004/03/12/88418.aspx
    It's really just an alias.
    Friday, April 14, 2006 6:45 PM
  • Thanks for the replies. I had a feeling that it was an alias but just wanted to be sure.

    I guess the smartest is to use string, since this does not require "using System", and is also common for most languages (when porting code to different language)

    Friday, April 14, 2006 10:11 PM
  • Yes - they are the same as the following IL disassembly shows:

    string s1;

    s1="232";

    00000029 mov eax,dword ptr ds:[0227307Ch]

    0000002f mov esi,eax

    s1.Remove(2);

    00000031 mov ecx,esi

    00000033 mov edx,2

    00000038 cmp dword ptr [ecx],ecx

    0000003a call 787455A0

    0000003f nop

    String s2;

    s2="4343";

    00000040 mov eax,dword ptr ds:[02273080h]

    00000046 mov edi,eax

    s2.Remove(2);

    00000048 mov ecx,edi

    0000004a mov edx,2

    0000004f cmp dword ptr [ecx],ecx

    00000051 call 787455A0

     

    However when it comes to using keywords like int, short, long etc, one has to keep in mind that there data type can change if the compiler changes from 32 bit to 64 bit, 64 bit to 128 bit etc. So if you are using any bitwise operation like shifts and rotates, you may get unexpected results. But for most practical purposes, it is safe to use int, short etc.

    Wednesday, June 06, 2007 8:16 AM
  •  Amit Basu wrote:

    However when it comes to using keywords like int, short, long etc, one has to keep in mind that there data type can change if the compiler changes from 32 bit to 64 bit, 64 bit to 128 bit etc. So if you are using any bitwise operation like shifts and rotates, you may get unexpected results. But for most practical purposes, it is safe to use int, short etc.

     

    In C# it's always safe to use the aliases because int is defined to be an alias for System.Int32 and not for the native size of the memory-address (as it is defined in C/C++). So also if compiler and systems change to 64 bit / 128 bit, the keyword int will always stay as an alias for System.Int32 so you don't have to worry about compatibility.

     

    Back to the string:

    I prefer to use the keyword string most of the time.

    Only when I'm using the static methods of the System.String class, I use sometimes the String class name instead of the keyword.

    For int, short, etc. I use the class name if I want to "highlight" the size of the variable used.

    Wednesday, June 06, 2007 11:14 AM
  • Their is no such difference between string and String, actually it let the user who is coming from C/C++ background and to make Understand that fimiliar data types names available in C# as it is available in c/c++. Both are defined in System namespace and contain same methods.

     

    Thursday, August 02, 2007 5:40 AM