views:

333

answers:

3

I am reading Effective C# by Bill Wagner. In Item 14 - Minimize Duplicate Initialization Logic, he shows the following example of using the new optional parameters feature in a constructor:

public MyClass(int initialCount = 0, string name = "")

Notice that he used "" instead of string.Empty.
He comments:

You'll note [in an example above] that the second constructor specified "" for the default value on the name parameter, rather than the more customary string.Empty. That's because string.Empty is not a compile-time constant. It is a static property defined in the string class. Because it is not a compile constant, you cannot use it for the default value for a parameter.

If we cannot use the string.Empty static in all situations, then doesn't that defeat the purpose of it? I thought that we would use it to be sure that we have a system-independent means of referring to the empty string. Is my understanding wrong? Thanks.

UPDATE
Just a follow up comment. According to MSDN:

Each optional parameter has a default value as part of its definition. If no argument is sent for that parameter, the default value is used. Default values must be constants.

Then we aren't be able to use System.Environment.NewLine either, or use newly instantiated objects as default values. I haven't used VS2010 yet, and this is disappointing!

+2  A: 

I never use string.Empty, I can't see the point of it. Maybe it makes it easier for people that are really new to programming, but I doubt it's useful even for that.

ho1
Maybe it prevents confusing `""` and `" "`, but I can't say that `" "` is all that common.
Greg
I'd suggest that anyone that can't tell the difference between those needs either better glasses or to lower the resolution of their screen. I've got bad eyesight and I can't recall ever making that mistake (and I do have to work with a lot of code that contains both of that).
ho1
+3  A: 

I think the idea behind string.Empty is it enhances readability. It is not like newline where there is any difference between how it is represented on different platforms. It's ashame it can't be used in a default parameter. However, it will not cause any issues if you port between Windows and something like Mono on Linux.

Tom Cabanski
I think you're probably right that the point is that some people consider `String.Empty` more readable . . . personally though, I think that's a little nutty. `""` is probably the most common string there is and everyone has seen it a billion times, so how is it unreadable? `String.Empty` is about as useful as if there were an `Int32.Zero`.
Tim Goodman
+7  A: 

As of the C# 2.0 compiler, there is very little point to String.Empty anyway, and in fact in many cases it's a pessimisation, since the compiler can inline some references to "" but can't do the same with String.Empty.

In C# 1.1 it was useful to avoid creating lots of independent objects all containing the empty string, but those days are gone. "" works just fine.

Andy Mortimer
Even in .NET 1.1 it wouldn't create "lots" of independent objects. I can't remember the details of the differences between 1.1 and 2.0 in this respect, but it's not like string literal interning was only introduced in 2.0.
Jon Skeet
Thanks for the clarification. I've had a look around and I haven't found a good summary of the changes in C# 2.0, although I'm sure I've read one previously. I did find a StackOverflow answer from 2008 with some links to more technical information. http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and
Andy Mortimer
I'll give this a nod, though I dislike saying there is little point to string.Empty as I use it quite heavily. I find it just looks cleaner, though that is my personal opinion. There are many places string.Empty cannot be use, and I have no problem using "" in those cases
xximjasonxx
I've used string.Empty only because it was the "recommended" thing to do, but I never really liked it. The literal "" seemed straight-forward and clear and with string interning, it's economical too.
tames