Contextual Keywords in C#

List of Contextual Keywords in C#

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 {

private event EventHandler event1;

public event EventHandler event2 {

add { samplePrivateEvent += value; }

remove { samplePrivateEvent -= value; }

}

}

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{

private int studRollNo;

public int ROLLNO {

get { return studRollNo; }

set { studRollNo = value; }

}

}

public class testClass {

public static void Main() {

student studObj = new student();

studObj.ROLLNO = 123;

Console.WriteLine( "ROLLNO: {0}", studObj.ROLLNO );

}

}

In this example, ROLLNO is a property with get and set accessors. You can access ROLLNO directly using the object studObj. When you say studObj.ROLLNO = 123, set accessor will be triggered and when you try printing it in Console.WriteLine as studObj.ROLLNO, its get accessor will be triggered.

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

partial class testClass { void testMethod1(); }

test2.cs

partial class testClass { void testMethod2(); }

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”;

var 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>

where Type1 : class

where Type2 : class, new() {

Type2 typ = new 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) {

for (int index = 0; index < count; index++) {

yield return index;

}

yield break;

}

public static void Main() {

foreach (int index in sampleYield(5)) {

Console.WriteLine(index);

}

}

In this example, yield keyword is used along with return and break keywords. When used along with return keyword, it doesn't 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.

References:

http://msdn.microsoft.com

http://dotnetperls.com

http://wiki.asp.net

Please be aware that the free essay that you were just reading was not written by us. This essay, and all of the others available to view on the website, were provided to us by students in exchange for services that we offer. This relationship helps our students to get an even better deal while also contributing to the biggest free essay resource in the UK!