0

Based on: C# Keywords

Keywords are predefined, reserved identifiers that have special meanings to the compiler.

and Based on: Contextual Keywords

A contextual keyword is used to provide a specific meaning in the code, but it is not a reserved word in C#.

For Keyword new we have multiple situation for use it, for example:

//One situation for create an instance of class:
StringBuilder sr=new StringBuilder();

//Another situation for method hiding in polymorphism subject of OOP:
public new void Foo()
{
   //Some Code
}

As you can see new keyword has Several meaning Depending on where it's used, then in my openion new must considered as Contextual Keyword and not keyword!

then, the Definition of Contextual keyword is wrong? or considering new as whole keyword? and why?

  • 3
    So it is var or new? You seem confused. – Euphoric Dec 22 '14 at 13:09
  • @Euphoric Thanks for the reminder , I was wrong! :) the post edited – kokabi Dec 22 '14 at 14:03
  • 2
    See also Eric Lippert's post on the topic: Reserved and contextual keywords – Brian Dec 22 '14 at 14:25
9

The question is not how many meanings it has, but whether or not it's reserved.

When Microsoft adds new features to C#, it sometimes need to add new keywords. This can compromise backward compatibility, since it invalidates old code that declares identifiers with the same name. To prevent this, Microsoft makes these new keywords contextual - they only serve as keywords when placed in specific places, places where you can't normally put identifiers.

So - async is a contextual keyword because older code might have declared a variable named async.

new was there from day one, so it doesn't have this problem - if your code declared a variable named new it wouldn't work even in the first version of C#. Therefore, there is no backwards compatibility issue here and new can be a regular keyword.

  • +1 for your excellent explanation, i don't have enough reputation for vote up, good and important point but i Wonder why this important issue has not been mentioned in MSDN. – kokabi Dec 22 '14 at 15:17
  • "C# 1.0 had contextual keywords get set value add remove for properties, indexers and events..." We had another version before C# 1.0 !? – kokabi Dec 22 '14 at 15:23
  • 1
    There goes my theory... – Idan Arye Dec 22 '14 at 16:36
  • 1
    I 'm satisfied with your answer , but now, I have another question. why c# 1.0 must have Contextual keywords? because there was no backwards compatibility issue at that time! – kokabi Dec 22 '14 at 16:57
  • 5
    Actually, that article does answer that question: "Because we could easily get away with making them contextual keywords, and it seemed likely that real people would want to name variables or methods things like get, set, value, add or remove. So we left them unreserved as a courtesy." – Idan Arye Dec 23 '14 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.