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
  • 3
    So it is var or new? You seem confused. Commented Dec 22, 2014 at 13:09
  • @Euphoric Thanks for the reminder , I was wrong! :) the post edited Commented Dec 22, 2014 at 14:03
  • 2
    See also Eric Lippert's post on the topic: Reserved and contextual keywords Commented Dec 22, 2014 at 14:25

1 Answer 1

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.

8
  • +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. Commented Dec 22, 2014 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 !? Commented Dec 22, 2014 at 15:23
  • 1
    There goes my theory... Commented Dec 22, 2014 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! Commented Dec 22, 2014 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." Commented Dec 23, 2014 at 10:10

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.