Optional parameters "must be a compile-time constant"

C#.Net

C# Problem Overview


I have a class divided in two partial files, like this:

public partial class PersonRepository : BaseRepository<Person>
{
    public static readonly string ColumnID = "ID";
    ...

and

public partial class PersonRepository : BaseRepository<Person>
{
    public List<Person> GetByCompany(int companyID, string sortExpression = ColumnID)
    {
    ...

But the compiler keeps saying that sortExpression "must be a compile-time constant". To me it seems a perfect compile-time constant, so I don't understand where the problem is.

C# Solutions


Solution 1 - C#

No, the expression PersonRespository.ColumnID is not classified as a compile-time constant. The expression "ID" is, but that's not what you're using as the default parameter.

In particular, if ColumnID is "just a normal field" then any references to it will be resolved as a field - so if you compile an assembly which refers to the field, then change the value and rebuild the assembly containing PersonRepository, the referring assembly will see that change.

If you change your declaration to:

 public const string ColumnID = "ID";

then it is a compile-time constant expression. That means in our previous scenario, the value of the constant is baked into any code that refers to it - and changing the value later without recompiling that referring code won't change the value used by that referring code.

See section 7.19 of the C# 4 language specification for more details about what counts as a constant expression.

Solution 2 - C#

You must declare your ColumnID as const.

The static readonly string will be instantiated when the class is first accessed in your code, and you could also initialize it with the return value of a static method, so it's not a compile-time constant for the compiler (even if in this case it obviously is for a person reading the code).

Solution 3 - C#

const is something declared with const keyword.

readonly field can be assigned in constructor and its not compile time constant. the code that you've written right now runs in initializer (before constructor). const fields are 'baked' in as constants.

Solution 4 - C#

change

public static readonly string ColumnID = "ID";

to

public const string ColumnID = "ID";

Solution 5 - C#

Try to change this

public static readonly string ColumnID = "ID";

to

public const string ColumnID = "ID";

Solution 6 - C#

> readonly

The value of a readonly field can be changed (in a constructor). You need a const.

Solution 7 - C#

Just for completeness, here are the three valid default values for an optional argument: ( from: MSDN: Named and Optional Arguments)

  1. a constant expression

  2. an expression of the form new ValType(), where ValType is a value type, such as an enum or a struct; (note: only the parameterless constructor can be used)

  3. an expression of the form default(ValType), where ValType is a value type.

Solution 8 - C#

public const string ColumnID = "ID";

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlessandroView Question on Stackoverflow
Solution 1 - C#Jon SkeetView Answer on Stackoverflow
Solution 2 - C#Paolo TedescoView Answer on Stackoverflow
Solution 3 - C#Muhammad Hasan KhanView Answer on Stackoverflow
Solution 4 - C#Øyvind BråthenView Answer on Stackoverflow
Solution 5 - C#ŻubrówkaView Answer on Stackoverflow
Solution 6 - C#AakashMView Answer on Stackoverflow
Solution 7 - C#BenView Answer on Stackoverflow
Solution 8 - C#archilView Answer on Stackoverflow