Saturday 17 January 2015

String vs string in C#

What's the difference between String and string in C#?

I don't recall why I started asking this question at interviews, I don't actually think it tells me anything other than whether the interviewee knows the difference between string and String and now I find that I can't stop myself from asking it.

I just want somebody to give me the right answer, one person, just one person would do.

Thus far, I've asked this question at eight interviews, yes, I know a pitifully small sample size, but still I would've expected to have found somebody who was aware of the difference or lack thereof. 

It's not like all interviewees have been fresh out of uni, in fact only one has been.

By far the most common answer (~90%) is :

String is class

For those of you wondering, what the difference is: There is none

What a bastard, right?

string is just an alias of System.String.

11 comments:

  1. Thats why I have trust issues.

    ReplyDelete
  2. That is my favorite question too and Jeff Richter gave very good argument to use String not string

    ReplyDelete
  3. String is an identifier, string is a keyword. So you can, for example, have a variable named String (not that you should), but not one named string.

    ReplyDelete
  4. Poor guy, if you'd have interviewed me, you'd have had more luck. I had this debate in a C# project at university, because I preferred "string" (looking like all other basic types) and a colleague preferred "String" (probably from Java conventions). So we checked it...

    ReplyDelete
  5. I was thinking this was a trick question hehe.

    ReplyDelete
  6. If you had asked me this, I'd have taken my pants off and started spinning my dick around.

    ReplyDelete
  7. If you use 'String', you need the specify the namespace. Because the compiler does know if you mean System.String or MyCoolClasses.String. But string is a language keyword, so the compiler doesn't need this info.

    Your IDE may include this namespace in a new source file by default, so you can forget about it most of the time.

    ReplyDelete
  8. As a sr dev you would have "gotten" me with this. But I would admit that I don't know which is what I would consider a success.

    ReplyDelete
  9. This is why experience is garbage. In college you learn as much as you can about everything so you're not a complete moron. In the job world you're trained very well to know one little niche very well and nothing else. Notice I said trained, not educated. Big difference. If you're lucky enough to be trained for it then you'll everything about it. Otherwise you'll never know the difference.

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete