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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
nanni nanniPosted Mar 25, 2016, 2:29 AM
Sam HobbsPosted Mar 19, 2016, 10:22 PM
Afzaal Ahmad ZeeshanPosted Mar 19, 2016, 8:32 AM
Anurag SinghPosted Mar 19, 2016, 7:06 AM
Anuja PawarPosted Jan 4, 2012, 3:20 AM
Sachin KadamPosted Dec 29, 2011, 5:28 AM
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
VulpesPosted Dec 29, 2011, 5:05 AM
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.
Sam HobbsPosted Dec 28, 2011, 8:40 PM
VulpesPosted Dec 28, 2011, 1:14 PM
Chintan RathodPosted Dec 28, 2011, 1:08 PM
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";