Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Enum.ToString not acceptable as const string for Attribute parameter string type?

[TL;DR] why const string SomeConstString = SomeEnum.OneEnumValue.ToString(); is not allowed in C# even if it can be processed in compile-time?

Below attribute definition is from Azure WebJobs SDK;

[AttributeUsage(AttributeTargets.Parameter)]
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public sealed class ServiceBusTriggerAttribute : Attribute
{
    private readonly string _queueName;
    private readonly string _topicName;
    private readonly string _subscriptionName;

    /// <summary>
    /// Initializes a new instance of the <see cref="ServiceBusTriggerAttribute"/> class.
    /// </summary>
    /// <param name="queueName">The name of the queue to which to bind.</param>
    public ServiceBusTriggerAttribute(string queueName)
    {
        ...
    }

And the usage of the code is as below;

public static void GetBapulActivityFromTopic(
        [ServiceBusTrigger("SomeConstString")] BrokeredMessage message,
        TextWriter log)
    {
        ...
    }

In above usage, I understand that "SomeConstString" should be const string type as the value has to be compile-time constant.

But why SomeEnum.OneEnumValue.ToString() cannot be a compile-time constant?

like image 236
Youngjae Avatar asked Jul 22 '26 02:07

Youngjae


2 Answers

Even though SomeEnum.OneEnumValue is a const, invoking a method on it is not a compile-time constant expression according to C# Language Specification. Here is a partial list of what is allowed:

  • Literals (including the null literal).
  • References to const members of class and struct types.
  • References to members of enumeration types.
  • References to const parameters or local variables
  • Parenthesized sub-expressions, which are themselves constant expressions.

Method invocations are not allowed in constant expressions. See section N.19 for a complete list.

If you are using C# 6, you can use nameof operator instead, which produces a compile-time constant:

const string SomeConstString = nameof(SomeEnum.OneEnumValue);
like image 139
Sergey Kalinichenko Avatar answered Jul 24 '26 17:07

Sergey Kalinichenko


Because Enum.ToString() is just a regular method call. It contains it's own logic inside. For example, if will check if given enum has [Flags] attribute and if yes - will convert enum value to string in a different way. C# compiler won't call arbitrary methods at compile time, so it cannot be compile-time constant.

And anyway it't not a very good idea to use enums in this case. For example, azure queue name can start with a number and contain dashes (as I remember), while enum cannot. Maybe today you don't use such queue names but who knows what will happen tomorrow. So it's better just define static class with public constants.

like image 45
Evk Avatar answered Jul 24 '26 18:07

Evk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!