Cannot use String.Empty as a default value for an optional parameter in C# - then what's the point?

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.

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.

1 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 Apr 23 '10 at 19:18 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.Stackoverflow.Com/questions/151472/… – Andy Mortimer Apr 23 '10 at 19:57 1 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 Apr 23 '10 at 19:58 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 Apr 23 '10 at 20:13 I've found that it's much easier to quickly identify an empty string with String. Empty than looking twice at "" to ensure it didn't have an apostrophe or something like that hidden in it. +1 for the explanation though.

– Chris Lively Apr 23 '107 at 21:17.

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.

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 Apr 23 '10 at 20:08.

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.

1 Maybe it prevents confusing "" and " ", but I can't say that " " is all that common. – Greg Apr 23 '10 at 19:19 3 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 Apr 23 '10 at 19:21.

I thought that we would use it to be sure that we have a system-independent means of referring to the empty string. In what way can the empty string vary from system to system? It's always a string with no characters!

I'd be really scared if I ever found an implementation where string. Empty == "" returned false :) This is not the same as something like Environment.NewLine. I want String.

Empty can be used as a default parameter in the next C# release. Well that's certainly not going to happen. While I would personally have liked a very different defaulting mechanism too, the way optional parameters work has been in .

NET since the start - and it always means embedding a constant into the metadata, so that the calling code can copy that constant into the call site if no corresponding argument is provided. Empty it's really pointless - using "" will do what you want; is it that painful to use the string literal? (I use the literal everywhere - I never use string.

That's what surprises me about this question - the complaint revolves around something which doesn't actually cause a real problem. It's for more important in cases where you want the default to be computed at execution time because it might actually vary. For example, I could imagine cases where you want to be able to call a method with a DateTime parameter and have it default to "the current time".

... but that's not always appropriate.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions