Skip to main content

How to Resolve Error "CS0599: Invalid value for named attribute argument 'argument'" in C#

The Compiler Error CS0599 is a restriction error related to Attributes. The message reads: "Invalid value for named attribute argument 'PropertyName'".

In C#, attributes allow you to attach metadata to your code (classes, methods, properties). When you use an attribute, you can set its public properties using Named Arguments (syntax: [MyAttribute(PropertyName = Value)]).

However, because attributes are baked into the compiled metadata of the assembly, the values assigned to these properties must be Compile-Time Constants. You cannot assign runtime variables, method calls, or unsupported types (like decimal or DateTime) to a named attribute argument.

This guide explains what constitutes a valid attribute value and how to fix assignments that violate this rule.

Understanding Attribute Constants​

When the compiler creates the metadata for an attribute, it must be able to write the exact value into the binary file. Therefore, attribute arguments are restricted to a specific subset of types:

  • Primitives: bool, byte, char, short, int, long, float, double.
  • Strings: string.
  • System Types: System.Type (via typeof).
  • Enums: Any public enum.
  • Arrays: Single-dimensional arrays of the types above.

CS0599 occurs specifically when you try to set a Named Argument (property setter) using a value that is either not one of these types or is not a constant.

note

CS0591 vs CS0599:

  • CS0591: Invalid value for a Positional argument (Constructor parameter).
  • CS0599: Invalid value for a Named argument (Property assignment).

Scenario 1: Assigning Variables instead of Constants​

This is the most common mistake. You define a variable (even a static readonly one) and try to assign it to an attribute property. To the compiler, static readonly is resolved at runtime, not compile-time.

Example of error:

using System;

[AttributeUsage(AttributeTargets.Class)]
public class AuthorAttribute : Attribute
{
public string Name { get; set; }
public int Version { get; set; }
}

public class Constants
{
// Calculated at runtime (when class loads)
public static readonly int CurrentVersion = 1;
}

// ⛔️ Error CS0599: Invalid value for named attribute argument 'Version'.
// 'Constants.CurrentVersion' is not a compile-time constant.
[Author(Name = "Alice", Version = Constants.CurrentVersion)]
public class MyBook
{
}

Scenario 2: Using Unsupported Types (Decimal, DateTime)​

Even if you use a literal value, certain types cannot be stored in attribute metadata. decimal is a common trap because it looks like a primitive number, but in .NET, it is a struct that requires initialization logic.

Example of error:

[AttributeUsage(AttributeTargets.All)]
public class CostAttribute : Attribute
{
public decimal Price { get; set; }
}

// ⛔️ Error CS0599: Invalid value for named attribute argument 'Price'.
// '19.99m' is a valid C# literal, but 'decimal' is NOT a valid attribute parameter type.
[Cost(Price = 19.99m)]
public class Product
{
}

Solution: Use Literals, Consts, and Primitive Types​

To fix the error, ensure that:

  1. The source value is a const field or a literal (e.g., 5, "Hello").
  2. The target property type is one of the valid attribute types (int, string, double, etc.).

Fix for Variables (Use const)​

Change static readonly to const.

public class Constants
{
// ✅ Correct: 'const' is baked into the binary at compile time.
public const int CurrentVersion = 1;
}

[Author(Name = "Alice", Version = Constants.CurrentVersion)]
public class MyBook { }

Fix for Unsupported Types (Use Alternatives)​

If you need to pass a decimal or DateTime, you must change the attribute to accept a double or string instead, and convert it internally.

[AttributeUsage(AttributeTargets.All)]
public class CostAttribute : Attribute
{
// We store it as double internally, or convert it in logic later
public double Price { get; set; }
}

// ✅ Correct: 'double' is supported in attributes.
[Cost(Price = 19.99)]
public class Product { }

Valid Array Syntax​

If assigning to an array property, you must use the array initializer syntax with constants.

public class TagsAttribute : Attribute
{
public string[] Tags { get; set; }
}

// ✅ Correct: Array initializer with string literals
[Tags(Tags = new string[] { "C#", "Cloud", "Web" })]
public class Post { }

Conclusion​

CS0599 enforces the rule that metadata must be static and simple.

  1. Check the Source: Are you using a variable? Change it to a const or a literal.
  2. Check the Type: Is the property a decimal, DateTime, or custom class? Change the attribute property to use string, double, or int.
  3. Check Context: Ensure you are assigning to a property (Name = Value).