dotnet / dotnet/csharpstandard

[return: attribute] on accessor that returns void

Open
#340 3 comments 0 reactions 0 assignees View on GitHub
status: needs triaging
Dominant language
C#
Stars
815
Forks
99
Avg merge
1d 14h
Merged PRs (30d)
16

Description

Microsoft's documentation on [Attribute specification](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/attributes#attribute-specification) and Microsoft C# compilers allow a `return` _attribute_target_specifier_ in the following contexts, where ECMA-334 5th edition § 22.3 (Attribute specification) does not allow it:

- `add` accessor of an event
- `remove` accessor of an event
- `set` accessor of a property
- `set` accessor of an indexer

```C#
using System;

public class Class1
{
public event EventHandler Event
{
[return: Demo] add { }
[return: Demo] remove { }
}

public int this[int index]
{
get { return 0; }
[return: Demo] set { }
}

public int Property
{
get { return 0; }
[return: Demo] set { }
}
}

[AttributeUsage(AttributeTargets.ReturnValue)]
public sealed class DemoAttribute : Attribute
{
}
```

Because `return:` attributes are generally allowed on methods that return `void`, this feature does not seem to cause any semantic problems.

The following C# compilers allow these attribute specifications even with `/langversion:ISO-1`:

- Microsoft (R) Visual C# Compiler version 2.10.0.0 (b9fb1610)\
Copyright (C) Microsoft Corporation. All rights reserved.
- Microsoft (R) Visual C# Compiler version 4.8.4084.0\
for C# 5\
Copyright (C) Microsoft Corporation. All rights reserved.
- Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.9149\
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727\
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

I don't know whether this feature has been formally proposed in . Anyway, it has already been implemented and documented, so perhaps it should be standardized.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.