logoalt Hacker News

twoodfintoday at 3:11 PM5 repliesview on HN

How does C# the language or C# the language standard evolution process accommodate a new keyword with such a generic name? Is it context-dependent?


Replies

janjonestoday at 3:21 PM

Yes, it's contextual. There is more details in this section of the article: Naming Conflicts with Existing Class Members

show 1 reply
estimator7292today at 3:33 PM

It's been a while, but from memory I think C# allows you to override keywords and use them as variable names when prefixed with @

The compiler knows what you're doing. A keyword like 'field's inside a function's braces just isn't valid. Putting 'field' after a type name in a variable declaration makes as much sense as 'private int class;'

kkukshteltoday at 3:19 PM

This is the first time they've done this in a long time fwiw. So the answer is "they usually never worry about this because it never happens".

That said, they will also throw compiler warnings in console during build if you are using an all lowercase word with some small number of characters. I don't remember the exact trigger or warning, but it says something like "such words may be reserved for future use by the compiler" to disincentivize use.

kgtoday at 7:23 PM

Historically every time new keywords are added, they try to make them contextual so that existing code won't break. 'await' and 'yield' are both examples where a new keyword was added without (generally) breaking existing code - they're only keywords in specific contexts and the way you use them ensures that existing code won't parse ambiguously, AFAIK.

lillecarltoday at 3:16 PM

Yes, you have to use field as the backing variable name in a property. The article is pretty clear about its usage.