Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are contextual keywords in C#?
In C#, contextual keywords are special identifiers that have reserved meaning only in specific contexts. Unlike regular keywords, they can still be used as variable names or identifiers when not in their special context. Common examples include get and set in properties, where in LINQ queries, and partial in class definitions.
Contextual keywords provide flexibility by allowing the same word to serve as both a keyword and an identifier depending on the context in which it appears.
Syntax
Contextual keywords are used within their specific contexts. Here are some common patterns −
// Property accessors
public int Value {
get { return value; }
set { value = value; }
}
// LINQ query expressions
from item in collection
where condition
select item
// Partial classes
partial class MyClass {
// class members
}
Contextual Keywords Table
| Keyword | Context | Purpose |
|---|---|---|
| get, set | Properties | Define property accessors |
| add, remove | Events | Define custom event accessors |
| partial | Types/Methods | Split class or method definitions across files |
| where | Generic constraints, LINQ | Specify type constraints or filtering conditions |
| value | Property setters | Represents the assigned value |
| yield | Iterator methods | Create iterator implementations |
| async, await | Asynchronous programming | Define and consume asynchronous operations |
Using Contextual Keywords in Properties
The most common use of contextual keywords is in property definitions with get and set −
using System;
class Person {
private string name;
private int age;
public string Name {
get { return name; }
set {
if (!string.IsNullOrEmpty(value))
name = value;
}
}
public int Age {
get { return age; }
set {
if (value >= 0)
age = value;
}
}
}
class Program {
public static void Main() {
Person person = new Person();
person.Name = "Alice";
person.Age = 25;
Console.WriteLine($"Name: {person.Name}");
Console.WriteLine($"Age: {person.Age}");
// get and set can be used as variables outside property context
string get = "This is allowed";
string set = "This is also allowed";
Console.WriteLine($"Variables: {get}, {set}");
}
}
The output of the above code is −
Name: Alice Age: 25 Variables: This is allowed, This is also allowed
Using Contextual Keywords in LINQ
LINQ queries use several contextual keywords like from, where, select, and orderby −
using System;
using System.Linq;
class Program {
public static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
var evenNumbers = from num in numbers
where num % 2 == 0
orderby num descending
select num;
Console.WriteLine("Even numbers in descending order:");
foreach (int num in evenNumbers) {
Console.WriteLine(num);
}
// These contextual keywords can be used as variables
string where = "condition";
string select = "choose";
string from = "source";
Console.WriteLine($"\nAs variables: {from}, {where}, {select}");
}
}
The output of the above code is −
Even numbers in descending order: 10 8 6 4 2 As variables: source, condition, choose
Using Partial Classes
The partial contextual keyword allows splitting class definitions across multiple files −
using System;
// First part of the partial class
partial class Calculator {
public int Add(int a, int b) {
return a + b;
}
public int Subtract(int a, int b) {
return a - b;
}
}
// Second part of the partial class
partial class Calculator {
public int Multiply(int a, int b) {
return a * b;
}
public int Divide(int a, int b) {
return b != 0 ? a / b : 0;
}
}
class Program {
public static void Main() {
Calculator calc = new Calculator();
Console.WriteLine($"Add: {calc.Add(10, 5)}");
Console.WriteLine($"Subtract: {calc.Subtract(10, 5)}");
Console.WriteLine($"Multiply: {calc.Multiply(10, 5)}");
Console.WriteLine($"Divide: {calc.Divide(10, 5)}");
// partial can be used as a variable outside class context
string partial = "This works as a variable name";
Console.WriteLine($"Variable: {partial}");
}
}
The output of the above code is −
Add: 15 Subtract: 5 Multiply: 50 Divide: 2 Variable: This works as a variable name
Complete List of Contextual Keywords
| Contextual Keywords | |||
|---|---|---|---|
| add | alias | ascending | async |
| await | by | descending | dynamic |
| equals | from | get | global |
| group | into | join | let |
| nameof | on | orderby | partial |
| remove | select | set | unmanaged |
| value | var | when | where |
| yield | |||
Conclusion
Contextual keywords in C# provide language flexibility by having special meaning only in specific contexts while remaining usable as regular identifiers elsewhere. They are essential for properties, LINQ queries, partial classes, and many other C# language features, making the syntax more readable and expressive.
