Consider this scenario:
public class C
{
private int _foo;
public int Foo
{
get { return _foo; }
[Obsolete("Modifying Foo through the setter may corrupt hash tables. "
+ "Consider using the method 'ModifyFoo' instead.")]
set { _foo = value; }
}
public C ModifyFoo( int foo )
{
// return a new instance of C
}
}
Which doesn't compile:
error CS1667: Attribute 'Obsolete' is not valid on property or event accessors. It is only valid on 'class, struct, enum, constructor, method, property, indexer, field, event, interface, delegate' declarations.
Applying attributes to specifically the accessors is perfectly fine for any other attribute (provided that AttributeTargets.Method
is set up in its usages, which is true for ObsoleteAttribute
).