Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

In the .NET Framework, at least in the C# language, we have two "versions" of the string type:

  • "string"
  • "String"

It appears that they are interchangeable, but are they really? If they are not interchangeable, is it generally better to use one instead of the other, and under what circumstances?

share|improve this question
1  
Similar to this question: stackoverflow.com/q/6000517/6504 –  Agent_9191 Apr 11 '12 at 21:24
2  
Stylistically, I tend to use the lower-case version when I'm referring to the type, and the upper-case version when I'm calling a static method of System.String. –  Joel Mueller Apr 11 '12 at 22:38
3  
Identical to stackoverflow.com/questions/7074/…. –  zzzzBov Apr 12 '12 at 4:30
2  
string does not exist in .net, it only exists in C#. –  CodesInChaos Apr 12 '12 at 17:41
1  
Although this might be a bit more suitable for Stack Overflow, I don't think it's completely off topic for Programmers, and since Stack Overflow already has at least one duplicate there wouldn't be much point in migrating this. So please stop flagging / close voting to migrate, if you feel this should be closed, that's cool, but it should be closed not migrated. –  Yannis Rizos Apr 13 '12 at 8:58
add comment

6 Answers

up vote 25 down vote accepted

Well according to the MSDN

string is an alias for String in the .NET Framework.

Where "String" is in fact System.String.

I would say that they are interchangeable and there is no difference when and where you should use one or the other.

It would be better to be consistent with which one you did use though.

share|improve this answer
1  
@RiceFlourCookies - indeed, but I am quoting from the MSDN there :) –  ChrisF Apr 11 '12 at 21:38
    
for some reason I had it in my head that String was a class wrapper around a value typed string and this leads to the idea of boxing... am i totally wrong here? –  Aaron Anodide Apr 11 '12 at 22:30
    
@Gabriel No, String (and hence string by alias) are reference types. –  Kris Harper Apr 11 '12 at 22:57
    
@Gabriel: doesn't Java do that with stuff like byte vs Byte? –  whatsisname Apr 12 '12 at 4:23
    
Is the same also true for Double vs double, and other data types? –  Gavin Coates Apr 12 '12 at 9:18
show 2 more comments

As pointed out, they are the same thing and string is just an alias to String.

For what it's worth, I use string to declare types - variables, properties, return values and parameters. This is consistent with the use of other system types - int, bool, var etc (although Int32 and Boolean are also correct).

I use String when using the static methods on the String class, like String.Split() or String.IsNullOrEmpty(). I feel that this makes more sense because the methods belong to a class, and it is consistent with how I use other static methods.

share|improve this answer
    
We use this convention at work as well, and I like it. My brain automatically makes the connection. –  SBI May 1 at 11:18
add comment

I'd suggest using lowercase string. Uppercase String will use the first class named String it finds in the imported namespaces ... which is generally System.String. It's possible for your own String class to be used instead.

share|improve this answer
3  
If you have two different imported types with the same name, and you use that name without namespace qualification, you will get an error. –  svick Apr 11 '12 at 22:02
    
It's possible to trick String into working, it's never possible to trick string. –  insta Apr 11 '12 at 22:04
    
Still, I don't think that's a good enough reason to use string. –  svick Apr 11 '12 at 22:05
4  
How is "one might break" vs. "one will never break" with all else being identical not a good enough reason? –  insta Apr 11 '12 at 22:24
add comment

string is a C# keyword which is simply an alias for the System.String

..but as it's a keyword, your syntax colouring editor/IDE will show it in keyword colour, same as int, double, if, else, etc. Stylistically, you may prefer to use the keyword rather than the System.String type (or just String after you've done using System;).

This is a niche point though, as although bool, char, int, double and string are all keyword aliases for system types, string is an odd-one-out as it is a reference type and the others are value types.

share|improve this answer
3  
object is a keyword alias for a reference type, too –  nikie Apr 12 '12 at 9:24
    
Oh yes, I forgot all about that one - thanks for reminder. –  JBRWilkinson Apr 12 '12 at 12:22
add comment

string is just an alias for String, they're ultimately the same.

You can use whichever you prefer, although I generally use string when I'm planning on using it much like a primitive data type (similar to int, float, or bool) and String when I'm planning on using it as an object/class. For example:

string foo = "   ";
if (String.isNullOrWhitespace(foo)) 
{
    // ...
}

In the case above, I'm planning on using foo simply to store some text (effectively as a primitive), so I use string; however, when I call String.isNullOrWhitespace(string) I use String. I do this, because generally the first letter of class names are capitalized (String) while the first letter of primitives (such as int) are not.

share|improve this answer
add comment

String is used with namespace System.String, string is used C# compiler specification, moreover System.String is reference type value. You are preferred to deal with System objects, string for literals purposes only. System and system both working is same.

string TC = new System.String('/', 5);

String is used for comparing, equal and other purposes as well.

share|improve this answer
3  
this doesn't seem to offer anything substantial over prior 6 answers –  gnat May 1 at 11:48
add comment

protected by gnat May 1 at 11:48

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.