10
Answers

Difference between string & String in C# .Net

Photo of SIVA

SIVA

13y
22.7k
1
Hi All,
Can anybody explain the difference between string & String in .Net
I am getting confuse with them
please clarify my confusion with small examples

Answers (10)

4
Photo of Chintan Rathod
NA 5.5k 1.9m 13y
In simple, there is no difference between string and String. string is a alias created by Microsoft for System.String
To go in detail these are the differences..

1) The string type represents a sequence of zero or more Unicode characters. string is an alias for String in the .NET Framework.

2) 'string' is the intrinsic C# datatype, and is an alias for the system provided type "System.String". The C# specification states that as a matter of style the keyword ('string') is preferred over the full system type name (System.String, or String).

3) A String object is called immutable (read-only) because its value cannot be modified once it has been created. Methods that appear to modify a String object actually return a new String object that contains the modification. If it is necessary to modify the actual contents of a string-like object

4) we can use 'String' to use system-defined methods, such as String.Compare etc. They are originally defined on 'System.String', not 'string'. 'string' is just an alias in this case.

Example,

string
place = "world";
string greet = String.Format("Hello {0}!", place);

Thank you.



1
Photo of Anuja Pawar
NA 1.7k 105k 13y
Hi Siva,
string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object. e.g.

string place = "world";
Likewise, I think it's generally recommended to use String if you need to refer specifically to the class. e.g.

string greet = String.Format("Hello {0}!", place);
This is the style that Microsoft tends to use in their examples.
1
Photo of Vulpes
NA 98.3k 1.5m 13y
Yes, I've seen it asked on many occasions in the past.

This is understandable because having been taught that C# is case sensitive, 'string' and 'String' appear to be breaking this rule which, of course, isn't really the case.
0
Photo of nanni nanni
NA 86 8.1k 9y
1)string and String both are same. 2)Both are derived from system.String 3)string is an alias for String ---Main use is coding convention level any of the methods of the string class take the support of Upper case String Ex:string first name="Nanneswara Reddy"; string last name="S"; //string fullname=string.Concat(firstname,lastname); use this: string fullname= String.concat(firstname,lastname); any of the methods of string class take Sting.
0
Photo of Sam Hobbs
NA 28.7k 1.3m 9y
Anurag, please refer to the documentation.
 
First, when I put "string s;"  into Visual Studio and hover over it I get (without the color):
 
 
That says that string is System.String, right?
 
The documentation is at https://msdn.microsoft.com/en-us/library/362314fe.aspx. The second sentence says "string is an alias for String in the .NET Framework.".
 
If you have something saying there is a difference then please share. 
0
Photo of Afzaal Ahmad Zeeshan
NA 36k 2m 9y
@Anurag:
 
String and string are similar in their definitions. The objects that are created using
string name = "Afzaal Ahmad Zeeshan";
Upon type checking, would always result in System.String.
Console.WriteLine(name.GetType().ToString()); 
Which means that "string" is just the alias around the String type. There is no other difference. "String" can also not be used as an identifier, which means, you cannot use it as:
string String = "My String";
This would give an error. Because, String is already defined as a class in the System namespace. To use this, you would have to remove the reference to System namespace itself, which would make your program make very little sense. :-)  
0
Photo of Anurag Singh
NA 74 5.3k 9y
Hi All, 
 
First of All, both(string & String)  are not same. 
There is a difference:
String is not a keyword and it can be used as Identifier whereas string is a keyword and cannot be used as Identifier.
 
And
 
Mr. Sachin,
Try to understand the question .
 
Anuraag Singh 
0
Photo of Sachin Kadam
NA 11 28.1k 13y

Hi,

Both String and StringBuilder are classes used to handle strings.

The most common operation with a string is concatenation. This activity has to be performed very efficiently. When we use the "String" object to concatenate two strings, the first string is combined to the other string by creating a new copy in the memory as a string object, and then the old string is deleted. This process is a little long. Hence we say "Strings are immutable".

When we make use of the "StringBuilder" object, the Append method is used. This means, an  insertion is done on the existing string. Operation on StringBuilder object is faster than String operations, as the copy is done to the same location. Usage of StringBuilder is more efficient in case large amounts of string manipulations have to be performed.

Refer following link with example :

http://www.codeproject.com/KB/cs/StringBuilder_vs_String.aspx



0
Photo of Sam Hobbs
NA 28.7k 1.3m 13y
This same question was asked a month or two ago.
0
Photo of Vulpes
NA 98.3k 1.5m 13y
As Chintan said, the C# built-in type 'string' is just an alias for the System.String type in the .NET Framework.

Consequently, if you have 'using System;' in your program, you can use 'string' or 'String' interchangeably. Here's an example:

using System;

class Test
{
    static void Main()
    {
       string s = "Siva";
       String t = "Siva";
       Console.WriteLine(s == t); // True
       Console.ReadKey();
    }
}