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
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Chintan RathodRank #516 · 4.8K reputationPosted Dec 28, 2011, 8:08 AM EST
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.VulpesRank #55 · 29.2K reputationPosted Dec 29, 2011, 12:05 AM EST
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.
Anuja PawarRank #1,404 · 1.2K reputationPosted Jan 3, 2012, 10:20 PM EST
VulpesRank #55 · 29.2K reputationPosted Dec 28, 2011, 8:14 AM EST
Sam HobbsRank #107 · 20.5K reputationPosted Dec 28, 2011, 3:40 PM EST
Sachin Kadam11 reputationPosted Dec 29, 2011, 12:28 AM EST
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
Anurag Singh131 reputationPosted Mar 19, 2016, 3:06 AM EDT
Afzaal Ahmad ZeeshanRank #60 · 27.6K reputationPosted Mar 19, 2016, 4:32 AM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Mar 19, 2016, 6:22 PM EDT
nanni nanni91 reputationPosted Mar 24, 2016, 10:29 PM EDT