Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:42:42+00:00 2026-05-26T21:42:42+00:00

I have custom attribute defined like so: [AttributeUsage(AttributeTargets.Field)] public class EnumDisplayAttribute : Attribute {

  • 0

I have custom attribute defined like so:

  [AttributeUsage(AttributeTargets.Field)]
  public class EnumDisplayAttribute : Attribute
  {
    public string Description { get; private set; }
    public string Code { get; private set; }

    public EnumDisplayAttribute(string description = null, string code = null)
    {
      Description = description;
      Code = code;
    }
  }

Both constructor parameters are optional.

When using this attribute on a field like so

  public enum TransactionType
  {
    [EnumDisplay(code: "B")] 
    Bill,
    [EnumDisplay(description: null, code: "C")]
    CashReceipt,
  }

I don’t see any squigglies in the code editor but I see a vague error without any File Line number of column. The error message is:

error CS0182: An attribute argument must be a constant expression, typeof expression
or array creation expression of an attribute parameter type

Clicking on the error does nothing. That is, you don’t get navigated to the error site (obviously, since there is no line number and column).

even if I set up the attribute like so:

[EnumDisplay("This is a Bill")] 

The compiler doesn’t like it.

Effectively, I am forced to provide both parameters (named or not) in order to use this attribute as an attribute.

Of course if I use this attribute as a regular class like so:

var enumDisplayAttribute = new EnumDisplayAttribute();
enumDisplayAttribute = new EnumDisplayAttribute(description: "This is a Bill");
enumDisplayAttribute = new EnumDisplayAttribute(code: "B");
enumDisplayAttribute = new EnumDisplayAttribute(description: "This is a Bill", code: "B");
enumDisplayAttribute = new EnumDisplayAttribute("This is a Bill", "B");
enumDisplayAttribute = new EnumDisplayAttribute("This is a Bill");

The compiler will accept any one of the above “styles”.

Surely, I’m missing something or my brain is just not working.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:42:42+00:00Added an answer on May 26, 2026 at 9:42 pm

    Optional parameters were added to C# after optional values for attributes already existed in C#. Therefore, for optional attribute parameters, you should fall back to the attribute-specific syntax:

    [AttributeUsage(AttributeTargets.Field)]
    public class EnumDisplayAttribute : Attribute
    {
        public string Description { get; set; }
        public string Code { get; set; }
    
        public EnumDisplayAttribute()
        {
        }
    }
    
    public enum TransactionType
    {
        [EnumDisplay(Code = "B")] 
        Bill,
        [EnumDisplay(Description = null, Code = "C")]
        CashReceipt,
    }
    

    As you see, the end-result is effectively the same, but instead of using named arguments, you are using named properties (where syntax like [EnumDisplay(Description = null, Code = "C")] is only possible in attribute declarations).

    Another way to think of it is that attribute declarations “borrowed” its syntax from method/constructor invocations, but attribute declarations are not in themselves method invocations, so they don’t get all the same features as methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class which is marked with a custom attribute, like this: public
I have two custom attributes defined like so: internal class SchemaAttribute : Attribute {
I have a field with a custom attribute on it, that looks like this:
I have a custom attribute which can be assigned to a class, [FooAttribute] .
For my custom view I have also defined a custom attribute for keep id
I have defined a custom html attribute data-something-something. In my view I use an
Right now, I have a class defined as such: public class Task { public
I have defined a custom Sharepoint list for special attributes related to a software
I have a custom attribute and I need to have the name of the
I have a custom validation attribute derived from action filter attribute. Currently the attribute

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.