anjum
anjum

Reputation: 2541

What is the difference between "string" and "String"?

What is the difference between these two declarations?

string str;
String str;

Upvotes: 3

Views: 633

Answers (6)

theburningmonk
theburningmonk

Reputation: 16051

They are the same, no difference, string is simply an alias for the System.String type, there are other similar cases in C#, like int and System.Int32, long and System.Int64 (see another related question)

Curiously though, whilst you can use alias in your code in place of the fully qualified types, you still need to know the underlying type when you use the Convert class because there's no ToInt or ToLong methods but only ToInt32 and ToInt64..

Upvotes: -1

Marc Gravell
Marc Gravell

Reputation: 1062905

In normal usage, string and String are identical; string is simply an alias for global::System.String. There are some edge-cases, though:

  • you need a using System; to use String - you don't for string
  • if you define a local class String {}, then String refers to that (this would be a silly thing to do, of course). You can't define a class called string (although @string is fine)

Upvotes: 23

heads5150
heads5150

Reputation: 7443

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

Upvotes: 2

Ani
Ani

Reputation: 113412

string is a c# keyword. String is the System.String .NET type.

The C# compiler provided by MS maps the string keyword to the System.String .NET type, so they are equivalent.

Upvotes: 2

Matěj Zábský
Matěj Zábský

Reputation: 17272

String is an alias for System.String object, so none.

System.String is a Common Type System type necessary for interaction with other .NET languages. "string" is just C# shortcut to this name (in the same way int is shortcut to System.Int32)

Upvotes: 1

Viper
Viper

Reputation: 2236

There is no difference. string (lower case) is just an alias for System.String.

Upvotes: 3

Related Questions