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?
Best Answer
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 namedasync
.new
was there from day one, so it doesn't have this problem - if your code declared a variable namednew
it wouldn't work even in the first version of C#. Therefore, there is no backwards compatibility issue here andnew
can be a regular keyword.