Skip to main content

How to Resolve Error "CS0617: 'name' is not a valid named attribute argument" in C#

The Compiler Error CS0617 is a specific error related to Attributes. The message reads: "'Name' is not a valid named attribute argument. Named attribute arguments must be fields which are not readonly, static, or const, or read-write properties which are public and not static."

In C#, applying an attribute involves two types of arguments:

  1. Positional Arguments: These correspond to the Attribute's constructor parameters.
  2. Named Arguments: These correspond to the Attribute's public, writable properties or fields.

This error occurs when you try to assign a value to a named argument (using Property = Value syntax), but the target member does not meet the strict requirements for being a named attribute argument (e.g., it is read-only, static, or private).

Understanding Attribute Argument Rules​

When you use the syntax [MyAttribute(MyValue = 5)], the compiler looks for a member named MyValue inside MyAttribute. For this assignment to work, MyValue must be:

  • Public: It must be visible to the generic attribute applicator.
  • Instance: It cannot be static.
  • Writable: If it is a property, it must have a set (or init) accessor. If it is a field, it cannot be readonly or const.

If any of these conditions are not met, the compiler raises CS0617.

Scenario 1: Read-Only Properties (Missing Setter)​

This is the most common cause. You define a property in your attribute class, but you omit the set accessor (perhaps to make it immutable), yet you try to set it via the property assignment syntax.

Example of error​

using System;

public class MetaDataAttribute : Attribute
{
// This property is Read-Only (Getter only)
public string Description { get; }

public MetaDataAttribute() { }
}

public class Program
{
// ⛔️ Error CS0617: 'Description' is not a valid named attribute argument
// because it is read-only.
[MetaData(Description = "Main Entry Point")]
static void Main() { }
}

Solution: Add a Setter or Init​

To use named arguments, the property must be writable.

using System;

public class MetaDataAttribute : Attribute
{
// ✅ Correct: Added 'set' (or 'init' in C# 9.0+)
public string Description { get; set; }

public MetaDataAttribute() { }
}

public class Program
{
// ✅ Correct: We can now assign to Description
[MetaData(Description = "Main Entry Point")]
static void Main() { }
}

Scenario 2: Static or Private Members​

Attributes operate on instances. When you place an attribute on a class, the runtime creates a new instance of that attribute. Therefore, you cannot set static fields (which belong to the class type, not the instance) or private fields (which are not accessible).

Example of error​

using System;

public class SettingsAttribute : Attribute
{
// Static fields cannot be named arguments
public static int Version = 1;

// Private fields cannot be named arguments
private bool _isEnabled;
}

public class Program
{
// ⛔️ Error CS0617: 'Version' is static.
[Settings(Version = 2)]
public void Run() { }
}

Solution: Use Public Instance Members​

Remove static and ensure the visibility is public.

using System;

public class SettingsAttribute : Attribute
{
// ✅ Correct: Public instance field (or property)
public int Version;
}

public class Program
{
// ✅ Correct
[Settings(Version = 2)]
public void Run() { }
}

Scenario 3: Confusing Constructor Parameters with Properties​

Developers often confuse Positional Arguments (Constructor parameters) with Named Arguments (Properties).

If your attribute has a constructor public MyAttr(string name), you must pass the name without an identifier (positional), or use the standard C# named argument syntax (name: "value"). You cannot use the property syntax (Name = "value") if there is no property named Name.

Example of error​

using System;

public class AuthorAttribute : Attribute
{
// Constructor takes a parameter 'name'
public AuthorAttribute(string name)
{
}
}

public class Book
{
// ⛔️ Error CS0617: 'name' is not a valid named attribute argument.
// The attribute has no PROPERTY called 'name'.
// It only has a constructor parameter.
[Author(name = "Alice")]
public string Title;
}

Solution: Use Constructor Syntax​

Use the standard constructor calling syntax.

public class Book
{
// ✅ Correct: Positional argument
[Author("Alice")]
public string Title;

// ✅ Correct: Named Argument for Constructor (C# 7.2+)
// Note the colon ':' instead of equals '='
[Author(name: "Alice")]
public string SubTitle;
}

Conclusion​

CS0617 ensures that attribute initialization is valid.

  1. Check Accessors: If assigning Prop = Value, ensure Prop has a set; or init; accessor.
  2. Check Visibility: Ensure the member is public.
  3. Check Static: Ensure the member is not static.
  4. Check Syntax: Distinguish between setting a Property (Prop = Val) and calling a Constructor (param: Val or just Val).