System.AttributeUsageAttribute.AllowMultiple Property

Gets or sets a Boolean value indicating whether more than one instance of the indicated attribute can be specified for a single program element.

Syntax

public bool AllowMultiple { get; set; }

Value

A bool where true indicates more than one instance of the attribute is permitted to be applied; otherwise, false. The default is false.

Remarks

An attribute that can be specified more than once for a program element is called a multi-use attribute. An attribute that can be specified only once is called a single-use attribute.

Example

Example #1:

The following example demonstrates the use of AttributeUsageAttribute.AllowMultiple. If AllowMultiple for an attribute is set to true, more than one of those attributes can be assigned to any given program element.

C# Example

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = true )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}

Example #2:

The following example demonstrates an error that is expected to be caught by compilers: the sample attempts to assign multiple instances of an attribute for which AllowMultiple was set to false.

C# Example

using System;

[AttributeUsageAttribute( AttributeTargets.Class |
                          AttributeTargets.Struct,
                          AllowMultiple = false )]
public class Author : Attribute {

  public Author(string name) { this.name = name; }
  public string name;
}

[Author( "John Doe" )]
[Author( "John Q Public" )]
class JohnsClass {

  public static void Main() {}
}

This should throw an error similar to:

error CS0579: Duplicate 'Author' attribute

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0