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.

2
  • 1
    possible duplicate of Why can't I use String.Empty as a default parameter value? Commented Jan 20, 2015 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 or even this - it took roughly 5 seconds to find these threads. Commented Jan 20, 2015 at 11:41

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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)
@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.
but you can't provide a parameterless constructor to a struct.
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.

1 Comment

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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.