From the original question:
I thought we would use it to make sure that we have a system-independent means of accessing an empty string.
How can an empty string differ from system to system? It is always a string without characters! I would really be scared if I found an implementation where string.Empty == "" returned false :) This is not the same as something like Environment.NewLine .
From Counter Terrorist bounty post:
I want String.Empty to be used as the default parameter in the next version of C # .: D
It’s good that, of course, this will not happen.
While I personally liked a very different default mechanism, the way to use additional parameters was in .NET from the very beginning - and this always means embedding the constant in the metadata, so that the calling code can copy this constant to the call site if the corresponding argument is not provided .
With string.Empty this is really pointless - using "" will do what you want; is it inconvenient to use a string literal? (I use literal everywhere - I never use string.Empty - but this is another argument.)
What surprises me on this issue is that the complaint revolves around something that does not actually cause a real problem. This is more important in cases where you want the default value to be computed at runtime, because it can really change. For example, I could imagine cases where you want to be able to call a method with the DateTime parameter and use its "current time" by default. At the moment, the only vaguely elegant workaround I know for this is:
public void RecordTime(string message, DateTime? dateTime = null) { var realDateTime = dateTime ?? DateTime.UtcNow; }
... but it does not always fit.
Finally:
- I doubt very much that this will ever be part of C #
- For
string.Empty this is pointless anyway - For other values ​​that really don't always have the same meaning, this can really be a pain
Jon Skeet Mar 03 '12 at 8:27 2012-03-03 08:27
source share