1

I am using default parameters but I'm getting error

Default parameter value for 'regularExpression' must be a compile time constant

Here is the method signature:

public static PIPE.DataTypes.BuyFlow.Entities.Question GetEmailAddressQuestion(string regularExpression = RegularExpressions.EmailAddressRegex, int rank = 1, bool isRequired = true)
{
}

And here is the property:

public static string EmailAddressRegex
{
    get {
            string emailAddressRegex = @"^[A-Za-z0-9\._%\+\-]+@([A-Za-z0-9\-]{1,40}\.)+([A-Za-z0-9]{2,4}|museum)$";
            return emailAddressRegex;
        }
}
Huma Ali
  • 1,759
  • 7
  • 40
  • 66

2 Answers2

4

It is like the error message says. Default parameters have to be constants (at compile time).

The Getter of EmailAddressRegex could return different values during runtime. That this is always the same value is not known to the compiler.

So change the EmailAddressRegex to a const string than the compiler error will be gone.

E.g.

public const string EmailAddressRegex = @"^[A-Za-z0-9\._%\+\-]+@([A-Za-z0-9\-]{1,40}\.)+([A-Za-z0-9]{2,4}|museum)$";
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
1

Default values for optional parameters are required to be compile-time constants.

In your case, a workaround would be:

public const string EmailAddressRegex = @"^[A-Za-z0-9\._%\+\-]+@([A-Za-z0-9\-]{1,40}\.)+([A-Za-z0-9]{2,4}|museum)$";                 

Here more details .. MSDN

Moumit
  • 8,314
  • 9
  • 55
  • 59