Reputation: 18759
Is there any way to specify an AttributeTargets.Method
attribute on an expression-bodied member in C# 6? Consider the following read-only property:
public bool IsLast { [DebuggerStepThrough] get { return _next == null; } }
The abbreviated synax would be:
public bool IsLast => _next == null;
But there appears to be nowhere to put the method attribute. None of the following work:
[DebuggerStepThrough]
public bool IsLast => _next == null; // decorates property, not method
public bool IsLast => [DebuggerStepThrough] _next == null; // error
public bool IsLast [DebuggerStepThrough] => _next == null; // error
DebuggerStepThrough
attribute--which is only given as an example here--in particular.
Upvotes: 5
Views: 242
Reputation: 18759
Answering my own question: The following syntax works but it is not quite as compact as the (non-working) attempts shown in the question.
public bool IsLast { [DebuggerStepThrough] get => _next == null; }
Upvotes: 1
Reputation: 29222
You can apply an AttributeTargets.Method
attribute to an expression-bodied method, but not to an expression-bodied property.
Upvotes: 2