2

If I type the following:

public Response GetArticles(string Filter = String.Empty)
{
    //Body
}

Visual Studio gives me this error:

Default parameter value for 'Filter' must be a compile-time constant

If I change the String.Empty to the classic "" it is fixed.

But I'm still curious about what is wrong with the String.Empty and its behavior.

Miguel A. Arilla
  • 5,058
  • 1
  • 14
  • 27
  • 1
    possible duplicate of [Why can't I use String.Empty as a default parameter value?](http://stackoverflow.com/questions/10825423/why-cant-i-use-string-empty-as-a-default-parameter-value) – Michal Hosala Jan 20 '15 at 11:38
  • Would be useful to use a google prior to asking questions like this. You would soon find out your question is a duplicate of [this](http://stackoverflow.com/q/10825423/3242721) or even [this](http://stackoverflow.com/q/507923/3242721) - it took roughly 5 seconds to find these threads. – Michal Hosala Jan 20 '15 at 11:41

2 Answers2

9

Why is String.Empty an invalid default parameter?

Because "Default parameter value for 'Filter' must be a compile-time constant". And String.Empty is not a constant but only static readonly. String literals like "Foo" are constants as implementation detail(i haven't found documentation).

Further read: Why isn't String.Empty a constant?

Quote from 10.4 Constants of the C# language specification:

The readonly keyword is different from the const keyword. A const field can only be initialized at the declaration of the field. A readonly field can be initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used. Also, while a const field is a compile-time constant, the readonly field can be used for runtime constants

Here is the MSDN quote according to optional parameters:

A default value must be one of the following types of expressions:

  • a constant expression
  • an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct
  • an expression of the form default(ValType), where ValType is a value type.

I'm suprised that you can use new ValType(), where ValType is a value type or struct. I didn't know that you can use the default constructor like new DateTime() but not new DateTime(2015,1,15). Learned something new from my own answer.

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • And MSDN doesn't say if `default(string)` is the answer that the OP is after. If that worked, it would probably be `null` anyway. Oh well. – Mr Lister Jan 20 '15 at 11:01
  • 1
    new Valtype() is allowed because structs can't have user defined parameterless constructor.it's provided by the compiler and the compiler knows what the result will be (e.g setting each field to it's default values) – Selman Genç Jan 20 '15 at 11:05
  • @Selman22: Thanks, but isn't it possible to write a struct which does weird things in the default constructor? I have also added that a string literal is a constant. However, i haven't found any documentation in MSDN or the language specification. – Tim Schmelter Jan 20 '15 at 11:07
  • 1
    but you can't provide a parameterless constructor to a struct. – Selman Genç Jan 20 '15 at 11:09
2

string.Empty is not a compile time constant. You can for example change it's value using Reflection. but "" empty string literal is constant and it's value is known at compile time so that's why it is valid.

Selman Genç
  • 100,147
  • 13
  • 119
  • 184
  • This pinpoints the answer to the actual question better, because it's shorter, but, ehm, I thought that we mere mortals weren't supposed to know that you could change the value of `string.empty`? – Mr Lister Jan 21 '15 at 13:33