What is the difference between string and String ? when is each one used ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gilit golit
    New Member
    • Feb 2011
    • 27

    What is the difference between string and String ? when is each one used ?

    HI ! I am expirienced C++ programmer , but new to C#

    1. What is the difference between string and String ? when is each one used ?

    2. I tried to use "ToCharArra y" , because I need the text in simple characters array.
    Code:
    	private System.Windows.Forms.TextBox UserName ;
            char [] name1;
            name1 = new char [64];
            name1 = this.UserName.Text.ToCharArray();
    But when tried to print it :
    Code:
      MessageBox.Show( name1.ToString ());
    It printed on the meesage box : "System.cha r [] " , which is not the text that is there.

    Code:
    MessageBox.Show( this.UserName.Text );
    worked fine

    3. I dont know what is wrong . ?

    4. Is c# characters array is like C character array or not ?

    Thanks
    Last edited by Niheel; Feb 7 '11, 11:33 PM.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    There is no difference between String and string, they're the exact same type.

    A char array is a little different in C# than it is in C++. In C++, when you define a char array, the variable is actually a pointer to the memory location of the first item in that array but in C# it doesn't work like that. The ToString call on a char array outputs the Type instead of the contents of the array.

    To output it, you'll have to loop through every element. It's also worth noting that strings in C# aren't null terminated, they are just contained by the array.

    So while C++ code might look like this... (forgive my rusty syntax, been a while. I think you can do this...)
    Code:
    char *s = "this is my string!\0";
    int i = 0;
    while (s[i++] != NULL) cout << s[i];
    cout << endl;
    ... in C# ...

    Code:
    char[] s = "this is my string!".ToCharArray()
    for (int i = 0; i < s.Length; i++)
      Console.Write(s[i]);
    Console.WriteLine();

    Comment

    Working...