
List of Contextual Keywords in C# (C Sharp)C# provides ten contextual keywords each having a specific meaning and purpose. They are: value, add, remove, get, set, global, partial, var, where and yield. When you code any of these keywords, to your surprise they will not be highlighted in blue color. This is because they are not part of reserved keywords of C#. This article will provide an overview on these ten contextual keywords.
Overview
of value, add, remove Contextual Keywords: The contextual
keyword value refers to the value assigned by the client code for a specific
property. For any class member, you can have get and set accessor methods.
Similarly for events, you have two methods as event accessors, they are
add and remove methods defined using add and remove contextual keywords.
Here is an example showing the usage of value, add and remove contextual
keywords: class testClass
{ In this example,
value passed while triggering the event is fetched using value keyword.
While attaching an event, add method will be triggered and while detaching
the event, remove method will be triggered. Overview
of get, set Contextual Keywords: If you declare
a class member as public, it will be directly accessible by other classes
using instantiation. This affects encapsulation. In earlier days you might
be writing your own getter and setter methods which will in turn return
and set values to the class member which is marked as private. Now, you
can accomplish the same concept in a much simpler way using properties.
Each property of a class has two accessors namely get and set. The method
get is used to return the value of the property and set is used to assign
value to the property. Here is an example to illustrate this scenario: public class
student{ Overview
of global Contextual Keyword: In C#, all
classes and other data structures you create have to be associated with
a namespace. Unless you specify a namespace, by default your class will
belong to global namespace. You can access members of global namespace
using global contextual keyword along with the operator ::.
If you have to access Console class of global namespace System, you can
code as: global::
System.Console.WriteLine("Your Specific Text"); If in case
you also code a new class called System, you can differentiate your class
from the global namespace using the code shown above. Overview
of partial Contextual Keyword: You can split
a class into multiple parts using partial keyword. Not just a class, even
struct and interface can be split using this keyword. For example, test1.cs
and test2.cs both together can define the class testClass as shown below: test1.cs test2.cs This means
that the class testClass contains two methods namely testMethod1() and
testMethod2(). Overview
of var Contextual Keyword: When you
do a local variable declaration, you specify the type and the variable.
Instead of type, you can also use the contextual keyword var which refers
to implicit variable declaration. Compiler will determine and associate
the type during execution. Here is an example: string s
= Hello; Both the
above lines of code are equivalent. In the above case, var is not really
necessary. You can use this keyword when you deal with anonymous types
or when you are not sure of the type. Overview
of where Contextual Keyword: When you
declare a generic class, you will not be aware of the type that is going
to be passed to the generic class. But there are situations when you have
to enforce constraints on the type. For example, the type passed to your
generic class should only be a class. Your class might have an additional
parameter which can be instantiated using new(). How do you enforce such
constraints? You can do so using the contextual keyword where as shown
below: class genericClass<Type1,
Type2> Overview
of yield Contextual Keyword: When you have to iterate over a block and
return an enumerable object, you have to write considerable amount of
code. This coding has to be done very carefully if not it will be error
prone. To avoid this situation and to create an enumerator object using
only one method, you have to use yield keyword. Here is an example to
demonstrate its usage: public static
IEnumerable sampleYield(int count) { public static
void Main() { In this example,
yield keyword is used along with return and break keywords. When used
along with return keyword, it doesnt return the value and end the
execution. Instead it puts the execution on hold and when the method is
called again for the next value of the enumeration, execution continues.
When used along with break keyword, it informs the end of execution and
the enumeration object is complete.
_______________________________________________________________________
FREE
Subscription
Subscribe
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |