Saturday, 1 October 2016

C# Keywords and Contextual Keywords You Must Know



C# Tips:
                                   Reserved Keywords/ Contextual Keywords in C#


e.g  of  c# Contextual keywords:

add,ascending,async,dynamic,equals,from,in,into,join,partial,remove,select,await,by,descending
get,global,group,let,on,orderby,set,value,var

this contextual keywords were added in .NET 3.0 and are mostly related to Linq, and where made contextual to avoid breaking some codes, many developers might have used same in their existing projects, so adding them as new keywords will cause serious problems to the existing applications, therefore there were made contextual.
e.g

//contextaware
Public class Context{

Public async void WontWork(){

String await = “Eze”;

}
/*
The above method will not work because anywhere the await keyword finds the async keyword as a method signature,it automatically becomes a reserve keyword, so in this context it will not work as a variable because it sees it as c# reserve keyword.
*/


Public void Work(){

String await = “Eze" // and here it works as a variable name
}
//in this method it will work perfectly as a variable because there is no async keyword, I hope //you understand this
}




All of the above cannot be used as a variable names,but If you ever want to use them,Then add the @ sign to it for example
String @class =”am in Primary school”;
That will work perfectly.

Summary: C# Keywords cannot be used as variable names, but c#  contextual keywords can be used when there are not in their useful context, as we’ve seen above.
if you still want to use the reserved keywords, add the @ sign a prefix to it.

Have any Question?
Comment!
Thank You for your time!

No comments:

Post a Comment