Cannot use String.Empty as default value for optional parameter

I am reading Effective C # Bill Wagner. In paragraph 14, “Minimizing Duplicate Initialization Logic,” he shows the following example of using the optional optional parameter function in the constructor:

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

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

You will note [in the above example] that the second constructor set the value "" for the default value for the name parameter, and not more than the usual string.Empty . This is because string.Empty not a compile time constant. This is a static property defined in the string class. Since it is not a compilation constant, you cannot use it for the default value for a parameter.

If we cannot use string.Empty statics in all situations, then does that not defeat its purpose? I thought we would use it to make sure that we have a system-independent means of accessing an empty string. I understand what is wrong? Thank.

UPDATE
Just a comment. According to MSDN:

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

Then we will not be able to use System.Environment.NewLine or use the newly created objects as default values. I have not used VS2010 yet and it is disappointing!

+75
c # optional-parameters
Apr 23 '10 at 7:11
source share
9 answers

As in the C # 2.0 compiler, in any case there is very little sense for String.Empty , and in fact in many cases it is pessimization, since the compiler can embed some links to "" , but cannot do the same with String.Empty .

In C # 1.1, it was useful to avoid creating many independent objects containing an empty string, but these days are gone. "" working fine.

+51
Apr 23 '10 at 7:16
source share

You have nothing to stop you from defining your own constant for an empty string if you really want to use it as an optional parameter value:

 const string String_Empty = ""; public static void PrintString(string s = String_Empty) { Console.WriteLine(s); } 

[As an option, one reason to prefer String.Empty over "" in general, which was not mentioned in other answers, is that there are various Unicode characters (zero-width stoppers, etc.) that are virtually invisible to the naked eye. Thus, something that looks like "" is not necessarily an empty string, while with String.Empty you know exactly what you are using. I admit that this is not a common source of errors, but it is possible.]

+50
Feb 25 '12 at 11:12
source share

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
+20
Mar 03 '12 at 8:27
source share

I never use string.Empty, I do not see the point. Perhaps this makes it easier for people who are really new to programming, but I doubt it is useful even for that.

+7
Apr 23 '10 at 19:13
source share

I think the idea of ​​string.Empty improves readability. This is not like a new line, where there is a difference between how it is presented on different platforms. Shamefully, it cannot be used in the default parameter. However, this will not cause any problems if you port between Windows and something like Mono on Linux.

+4
Apr 23 '10 at 19:15
source share

Like FYI, it seems that the same restriction is imposed on the values ​​passed to the attribute constructors - they must be constant. Since string.empty is defined as:

 public static readonly string Empty 

rather than the actual constant, it cannot be used.

+3
Feb 27 2018-12-12T00:
source share

If you really don't like string literals, you can use:

 public static string MyFunction(string myParam= default(string)) 
+1
Apr 05 '13 at 14:23
source share

I use string.Empty solely for readability.

If someone else needs to read / modify the code later, they know that I wanted to check or install something on an empty line. Using only "" can sometimes lead to errors and confusion, because I may have just forgot to put the line there.

For example:

 if(someString == string.Empty) { } 

against

 if(someString == "") { } 

The first if seems to me much more deliberate and readable. Because this is only preference, but I really do not see the train being broken to use "" instead of string.Empty .

0
Jun 22 '15 at 10:52
source share

Perhaps the best solution to this problem is to overload this method like this:

 public static void PrintString() { PrintString(string.Empty); } 
-one
Sep 26 '13 at 7:15
source share



All Articles