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 1096151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:14:20+00:00 2026-05-17T00:14:20+00:00

So I have a custom attribute, let’s call it MyCustomAttribute , which has a

  • 0

So I have a custom attribute, let’s call it MyCustomAttribute, which has a constructor like so:

public MyCustomAttribute(int? i)
{
   // Code goes here
}

and declares a property:

public int? DefaultProperty { get; set; }

Now if I wanted to use the property, I’d need to pass in an int or null, well that’s what I’d expect.

But this gives a compiler error:

[MyCustomAttribute(1, DefaultProperty = 1)]
public int? MyProperty { get; set; }

and so does this:

[MyCustomAttribute(null,DefaultProperty = null)]
public int? MyProperty { get; set; }

The error is: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type for both the constructor and the property.

Why is this? If I change the constructor to take an int, I can pass in 0, but not null, which sort of defeats the purpose of the value (which can sometimes be null)

  • 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-17T00:14:20+00:00Added an answer on May 17, 2026 at 12:14 am

    The reason why is that while both 0 and null are constant the actual expression needed to initialize the constructor parameter is not. Both require a conversion to be a valid int? expression. Under the hood it essentially generates the following

    [MyCustomAttribute(new Nullable<int>(0))]
    

    This expression not being constant is not legal as an attribute argument

    EDIT

    dtb asked if this is illegal in attribute values why is it legal to have a default parameter for a nullable parameter?

    void Example(int? x = null);
    

    What you need to consider is who / what is interpreting the values.

    For attributes arguments the value is interpreted by the CLR. The rules concerning legal attribute values haven’t really changed since 1.0. Nullable didn’t exist then hence the CLR doesn’t understand nullable initialization patterns.

    For default arguments the value is interpreted by the compiler at the call site of the method. The compilers understands nullable values and has a bit more room to work with as it can create non-constant expressions at the call site based on the values in IL.

    How does it actually work though? First the compiler will actually encode constants and null differently in IL

    // x = 0 
    [DefaultParameterValue(0)]
    // x = null
    [DefaultParameterValue(null)]
    

    At the call site the compiler examines these values and creates the appropriate (non-constant) expression for the parameter value.

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

Sidebar

Related Questions

I have a custom attribute which is multi select type and I want to
Let's say i have multiple properties with a Custom DataAnnotation Attribute of: [Objective]. I
I have a custom attribute and I need to have the name of the
I have this custom validation attribute for validating a collection. I need to adapt
I have a custom validation attribute derived from action filter attribute. Currently the attribute
I have a custom authorize attribute on my controllers and it is not being
Should I need to have explicit custom ID attribute as PK for each of
How do i select elements that have my custom data attribute data-validate with jQuery
In my asp.net mvc3 application, I have a custom Authorization Attribute as seen below.
I have assigned a custom data attribute to some circles added to the Raphael

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.