String vs string

Introduction

In this post I discuss what is the difference between String and string in C#? (notice the case). What are the guidelines for the use of each? And what are the differences?

What is String and string?

string is an alias for System.String. So technically, there is no difference. It’s like int vs. System.Int32.

About using the provided type aliases in C#, Jeffrey Richter quoted in his book CLR Via C#

I’ve seen a number of developers confused, not knowing whether to use string or String in their code. Because in C# the string (a keyword) maps exactly to System.String (an FCL type), there is no difference and either can be used.

for e.g.

I think it’s generally recommended to use String if you need to refer specifically to the class.
e.g.

One difference

String is a reserved word, but String is just a class name. This means that ‘string’ cannot be used as a variable name by itself.

If for some reason you wanted a variable called string, you’d see only the first of these compiles:

Reference

http://stackoverflow.com/questions/7074/whats-the-difference-between-string-and-string

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s