What is the Difference between String and string in C#?


Although I have intermediate knowledge of asp.net & C#, but I am still not clear with what's the difference between String (capital S) and string in C#?

When to use which one?

Example code:

string name="Pika";
String name ="Pika";

thanks


Asked by:- pika
0
: 2632 At:- 10/25/2017 11:36:55 AM
C# .net difference of String & string string







2 Answers
profileImage Answered by:- bhanu

According to MSDN ,string is an alias for String in the .NET Framework.Despite C# being a case-sensitive language you can see, whether you use “string” or “String,” the outcome is the same.

In simple, there is no difference between string and String. string is a alias created by Microsoft for System.String.

As we go into details, you can see these difference between them:

  • 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
  • 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).
  • use String when using the static methods on the String class, like String.Split() or String.IsNullOrEmpty()
  • use string to declare types - variables, properties, return values and parameters. This is consistent with the use of other system types - int, bool, var etc (although Int32 and Boolean are also correct).
1
At:- 10/26/2017 4:25:21 AM


profileImage Answered by:- neena

"string" keyword is an alias for System.String in the .NET, that means the String and string are equal, you can either one of them while writing code in C#

Syetem.String and string are compiled to System.String in Intermediate Language (IL), so the performance of both is also same, it just depends on your choice, which one to use.

  • Use string while referring to an object.
  • Use String while referring to the class.
1
At:- 10/27/2017 3:05:33 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use