C# - String or string

Associate
Joined
30 Mar 2004
Posts
1,148
Location
West Wing
Hi,

What exactly is the difference between declaring a string variable with the lower case version of string as opposed to String with a cap S? Whats the upper case 'String' for?

Thanks,
 
Associate
Joined
16 Oct 2006
Posts
560
Location
U.K.
Absolutely no difference, string is just an alias for System.String.

I think the recommendation is to use string, however, but it's purely stylistic.
 
Don
Joined
5 Oct 2005
Posts
11,156
Location
Liverpool
string is an alias for System.String. So technically, there is no difference. It's like int vs. System.Int32.

As far as guidelines, I think it's generally recommended to use string any time you're referring to an object.

e.g. string place = "world";

Stelly
 
Soldato
Joined
18 Oct 2002
Posts
15,412
Location
The land of milk & beans
I think the above have answered the question, but my personal preference is to use 'string' when dealing with the type, and 'String' when dealing with functions of the type. Eg:

Code:
string foo = "ABC"; // should really use var here and let the compiler determine the type, but for examples' sake...
string bar = "XZY";

var otherString = String.Format("Foo: {0}, Bar: {1}", foo, bar);
 
Associate
Joined
16 Oct 2006
Posts
560
Location
U.K.
I think the above have answered the question, but my personal preference is to use 'string' when dealing with the type, and 'String' when dealing with functions of the type. Eg:

Code:
string foo = "ABC"; // should really use var here and let the compiler determine the type, but for examples' sake...
string bar = "XZY";

var otherString = String.Format("Foo: {0}, Bar: {1}", foo, bar);

Interestingly, while there are Microsoft provided examples on MSDN of the above practice, using String would throw a violation from StyleCop.

The particular violation being for not using built-in type alias'.
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
I always use the aliases when I'm coding, though there are a few things that don't make too much sense for the aliases.
as an example Convert.ToInt64 returns something that I would define as a long, which doesn't feel quite right but I'll still stick to aliases.
 

AJK

AJK

Associate
Joined
8 Sep 2009
Posts
1,722
Location
UK
I always use the aliases when I'm coding, though there are a few things that don't make too much sense for the aliases.
as an example Convert.ToInt64 returns something that I would define as a long, which doesn't feel quite right but I'll still stick to aliases.

But a long is a 64-bit integer... not sure why that's confusing?
 
Soldato
Joined
18 Oct 2002
Posts
3,926
Location
SW London
But a long is a 64-bit integer... not sure why that's confusing?

It's not that it's confusing, but if the guidance is to use aliases in your code why is the method not called ToLong?
Especially when there are properties/methods called LongLength and LongCount() on the Array type and in LINQ respectively.
 

Pho

Pho

Soldato
Joined
18 Oct 2002
Posts
9,325
Location
Derbyshire
Code:
using WhyUseStringorstringWhenYouCanUseMegaString = System.String;

WhyUseStringorstringWhenYouCanUseMegaString awesome = "awesome";

Don't be a sheep :p.
 
Associate
Joined
10 Dec 2008
Posts
1,857
Location
Somewhere over there
But a long is a 64-bit integer... not sure why that's confusing?

It is for me when going back and forth between c# and c++ seeing as c++ treats a long as a 32-bit integer :p
string all the way for me now, used to using String for method calling but stopped to just have uniformity I think? Was a while ago now and kinda stuck heh.
 
Back
Top Bottom