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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:44:46+00:00 2026-05-28T13:44:46+00:00

These are the most common and only patterns I have seen so far: [AttributeFoo]

  • 0

These are the most common and only patterns I have seen so far:

[AttributeFoo]
[AttributeBar("Hello world!")]
[AttributeBaz(foo=42,bar="Hello world!")]
public class Example {}

The attribute syntax looks like you’re calling a constructor. And before C# supported optional and named arguments, named parameters for attributes were the only visible difference.

Does the C# compiler allow anything else? Like params arguments or object/collection initializers?

See also: Applying Attributes on MSDN

  • 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-28T13:44:46+00:00Added an answer on May 28, 2026 at 1:44 pm

    AFAIK, named parameters only permit integral types. Unfortunately i do not have a reference to back this up, I only learnt it through my own experimentation.

    When trying to use object initialisers, I got this error from the compiler:

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

    Although this documentation is a few years old, it has the reference information I was looking for:

    Attribute parameters are restricted to constant values of the
    following types:

    • Simple types (bool, byte, char, short, int, long, float, and double)
    • string
    • System.Type
    • enums
    • object (The argument to an attribute parameter of type object must be a constant value of one of the above types.) One-dimensional
      arrays of any of the above types

    So this works:

    //Test attribute class
    [AttributeUsage(AttributeTargets.All)]
    internal class TestAttribute : Attribute
    {
        public int[] Something { get; set; }
    }
    
    //Using an array initialiser - an array of integers
    [TestAttribute(Something = new int[]{1, 2, 3, 4, 5})]
    public abstract class Something
    

    Whereas this wouldn’t:

    //Test person class
    internal class Person
    {
        public string Name { get; set; }
    
        public Person(string name)
        {
            this.Name = name;
        }
    }
    
    //Test attribute class
    [AttributeUsage(AttributeTargets.All)]
    internal class TestAttribute : Attribute
    {
        public IEnumerable<Person> Something { get; set; }
    }
    
    //This won't work as Person is not an integral type
    [TestAttribute(Something = new Person[]{new Person("James")})]
    

    EDIT: just to elaborate, attributes form part of the metadata for constructs they are applied to (within the generated IL), thus the members of the attribute class must be determined at compile time; hence the restriction on attribute parameters to constant values.

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

Sidebar

Related Questions

What are the most common problems that can be solved with both these data
This question is similar to What is the most common design patterns for any
There are a few ways to get class-like behavior in javascript, the most common
I do most of my development in Common Lisp, but there are some moments
Like most web developers these days, I'm thoroughly enjoying the benefits of solid MVC
Most of the posts that I read pertaining to these utilities usually suggest using
I cannot find comparison of these parsing technique. Which one is most commonly used?
As we all know most apps have a data access layer, often using repository
Most projects have some sort of data that are essentially static between releases and
There are a few different common patterns for returning the result of a function

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.