Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the attribute target 'typevar' undocumented?

Tags:

c#

attributes

As is well known, in C# one can specify the target of a custom attribute specification, as in the example

[method: SomeDecoration]
[return: SomeOtherMark]
int MyMethod();

where the "targets" method: and return: help specify what element in the code the attribute belongs to.

According to the C# Language Specification, the following attribute targets exist:

  • Global ones:
    • assembly
    • module
  • Others:
    • field
    • event
    • method
    • param
    • property
    • return
    • type

Some of them, like field, are always redundant, since it is always clear what the attribute "sits" on without specifying them.

However there does exist (at least in the implementation and version of Visual C# I have here) an additional attribute target, namely:

  • typevar

which is allowed for example in the code

class MyGenericCollection<[typevar: HereYouSee] TItem>    // legal
{
}

The attribute target typevar, just like field and others, is never required.

My question: Does anyone know the historic reason why typevar: is not mentioned in the specification or documentation? Was this simply forgotten when the 2.0 version of the C# Language Specification was written? Or if it was not an oversight, why is it implemented at all?

like image 296
Jeppe Stig Nielsen Avatar asked Jan 02 '14 17:01

Jeppe Stig Nielsen


1 Answers

Actually, typevar attribute target is documented, but it seems that only in the standardized C# 2.0 Language Specification: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf#page=399

Neither C# 3.0, nor 5.0 Language Specification mentions typevar. (I didn't find C# 4.0 spec.)

So answering your second question, no, it wasn't forgotten in C# 2.0, but it is forgotten since then :) I think this must be an oversight, since the typevar attribute target is still (C# 5.0) valid.

like image 133
dbalogh Avatar answered Oct 08 '22 00:10

dbalogh