Glenn Slayden
Glenn Slayden

Reputation: 18759

method Attribute on C# expression-bodied member

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



[Edit:] Suggesting that this is not a duplicate of 'Skip expression bodied property in debugger' since this question asks about any method-suitable attribute in general, rather than just the DebuggerStepThrough attribute--which is only given as an example here--in particular.

Upvotes: 5

Views: 242

Answers (2)

Glenn Slayden
Glenn Slayden

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

Scott Hannen
Scott Hannen

Reputation: 29222

You can apply an AttributeTargets.Method attribute to an expression-bodied method, but not to an expression-bodied property.

Upvotes: 2

Related Questions