Difference between String and string in C#

For many C# newbies, it is a common confusion by seeing String and string. Both of them will be used as per individual preference and need.

String comes from a class System.String in .NET Framework where as string is an C# alias for System.String. As per the individual’s preference they use either String(Upper case) or string (lower case) as both work in the same way but not all the times.

We can use string do declare fields, properties etc that use the predefined type string, since the C# specification tells me this is good style. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
// can declare fields and  properties.
string name = "CSharp"
 
// can not use Pre-defined methods
string name = "CSharp";
name.Compare
 
// possible to declare variables , properties..
String  name = "CSharp";
 
//possible to use pre-defined methods
String  name = "CSharp";
name.Compare

Similarly there are C# aliases for types in .NET Framework, int is an C# alias for Int32 which is a type in .NET Framework. In the same way there are C# aliases for types in .NET Framework.

Share Article/Example:
  • DotNetKicks
  • DZone
  • StumbleUpon
  • Print
  • Add to favorites
  • Digg
  • del.icio.us
  • Twitter
  • Facebook
  • LinkedIn
  • Posterous
  • Slashdot

 

 
eXTReMe Tracker