1

Possible Duplicates:
In C# what is the difference between String and string
String vs string in C#

Hi,

Can anyone tell me the difference between the two lines of code below:

public const String sample = "Sample";
public const string sample2 = "Sample2";

Both "String" and "string" are of System.String.

Thanks in advance.

Community
  • 1
  • 1
Derin
  • 83
  • 2
  • 7
  • Same as [In C# what is the difference between String and string ](http://stackoverflow.com/questions/7074/in-c-what-is-the-difference-between-string-and-string). – Matthew Flaschen Nov 24 '10 at 06:56

9 Answers9

1

These data types are exactly the same, as string is just an alias for the class String. If you have a look, there are similar capitalized and non capitalized versions of int, float and similar classes.

Have a look here for a more detailed answer.

Community
  • 1
  • 1
Migwell
  • 18,631
  • 21
  • 91
  • 160
0

There are the same

string is an alias in the C# for .Net System.String, like int is for Int32, long is for int64 and etc. C# string replaced by System.String during compilation

Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
0

No difference what so ever - in fact you can write the following code:

String sample = "Sample";
string sample2 = sample;

Both maps to the same IL string type

Dror Helper
  • 30,292
  • 15
  • 80
  • 129
0

String vs string in C#

String stands for System.String and it is a .NET Framework type. string is an alias in the C# language for System.String. Both of them are compiled to System.String in IL (Intermediate Language), so there is no difference. Choose what you like and use that. If you code in C#, I'd prefer string as it's a C# type alias and well-know by C# programmers.

Community
  • 1
  • 1
KMån
  • 9,896
  • 2
  • 31
  • 41
0

String is CTS type but string is c# string object.

You can use String to any of dot net language.

Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59
0

both are the same string -> c# type which gets converted to String -> .net type

Rony
  • 9,331
  • 2
  • 22
  • 22
0

String is the .NET class for the CLR built-in string type. string is the C# language identifier that maps to the CLR String type. They are the same thing.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
0

"string" is actually an alias for System.String. They're the same.

Try:

typeof(string) == typeof(String) == typeof(System.String)
dnets
  • 1,022
  • 14
  • 11
0

Nothing really, in C# the type keywords actually are synonyms for the types. So int = System.Int32 short = System.Int16 and string = System.String.

They have the keywords because they are easier to remember and programmers coming from other languages like c/c++ would also be familiar with these types.

Anyway, look at the C# keyword reference and you can find these things out. This was taken from the string keyword reference.

The string type represents a string of Unicode characters. string is an alias for String in the .NET Framework. Strings are immutable--the contents of a string object cannot be changed after the object is created.

Singleton
  • 3,701
  • 3
  • 24
  • 37