Monday, January 19, 2009

String versus string in C#

A month ago I had a developer that I'm currently in the same project with questioning me about my use of string versus my use of String. She found it confusing that I didn't just use one of them consistently. I told her my idea, which is that I'm using String when accessing static members and string otherwise, but she still found it confusing.

I then decided to consult the IDesign C# Coding Standard, and looking there I found some inconsistency. I decided to ask them about it, and they seem to share my belief. See my mail and answer below.

From: Juval Lowy
Sent: den 19 december 2008 15:51
To: Tommy Bryntse
Subject: RE: Contacting IDesign

You should use String for the static members.

From: Tommy Bryntse
Sent: Friday, December 19, 2008 6:27 AM
Subject: Contacting IDesign

Hello IDesign!

I noticed an inconsistency in your C# Coding Standard.

In section 1.10 it says

Always use … string NOT String.

While in section 2.52 it says

Use String.Empty instead of "":

Myself I’ve always used String for accessing static members in string, and string
otherwise, just as your example. Section 1.10 was about to make me change that to
always use string, but after seeing section 2.52 I’m confused. What should I do?

Kind regards,



Unknown said...

It still doesn't answer as WHY?

Whats the reason behind it ?

Tommy said...

In my case I use string when I think of it as a simple type, just like I do with int, char, byte etc. Simple types aren't classes/objects tho, so when I decide to use a method declared in the underlaying .NET Framework type in C# I use the class name, that is String, Integer, Char, Byte etc. Hence I use "string" in declarations and "String" when using the static methods and properties, like "String.IsNullOrEmpty(string)" or "String.Empty".

Anonymous said...

Thanks Tommy for a short but sweet post!