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,
 
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.
 
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
 
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);
 
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'.
 
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.
 
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?
 
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.
 
Code:
using WhyUseStringorstringWhenYouCanUseMegaString = System.String;

WhyUseStringorstringWhenYouCanUseMegaString awesome = "awesome";

Don't be a sheep :p.
 
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