Contextual Keywords in C#: Everything you need to know

contextual keywords in c#

In C#, contextual keywords are a special type of keywords that provide specific meaning within a particular context in your code, but unlike regular keywords (for example- class, interface and int), they are not reserved words. This means you can use them as identifiers outside of that specific context.

These are some of the contextual keywords you should know:

  • and– The keyword and was introduced in C# 9 for pattern matching. It allows you to combine multiple patterns, requiring all of them to match for the code to proceed. In other words, It’s similar to && operator that we use in conditional expressions.
and keyword example
  • ascending– The keyword ascending is used specifically within query expressions. It indicates that you want to sort the results of the query in ascending order, from smallest to largest.
ascending keyword example
  • async & await– The async and await keywords are a powerful combination for writing asynchronous code. They allow your program to perform long-running operations without blocking the main thread, making your application more responsive and efficient. Once a method is declared as async, you can use await within its body.
async-await keyword example
  • dynamic– The dynamic keyword is used to declare variables or method parameters that can hold objects of any type at runtime. It bypasses static type checking at compile time, offering more flexibility but it also sacrifices some of the safety benefits of a statically typed language like C#.
dynamic keyword example
  • equals– The equals keyword is used to compare two objects for equality. It’s similar to == operator that we use in conditional expressions.
equals keyword example
  • into– The into keyword is used specifically within query expressions. It allows you to store the results of a group, join, or select clause into a new temporary variable. This variable can then be used for further operations within the query expression.
into keyword example
  • nameof– The nameof keyword is a handy tool introduced in C# 6.0. It allows you to get the name of a variable, method, type, or other program element as a string literal at compile time.
nameof keyword example
  • partial– The partial keyword is used to define a class, struct, or interface across multiple source files. This allows you to logically separate the functionality of a type while maintaining it as a single entity during compilation.
partial keyword example
  • var– The var keyword is used for implicit type declaration of variables. It allows the compiler to infer the data type of the variable based on the value assigned during initialization.
var keyword example
  • with– The with keyword was introduced in C# 10 for creating modified copies of objects. The with expression allows you to create a new object that’s a copy of an existing object, with specific properties or fields modified.
with keyword example

So these were some of the contextual keywords we use in C#. Some other notable contextual keywords include global, join, record and yield.

Do let me know in the comment section what other contextual keywords you know about. 

2 thoughts on “Contextual Keywords in C#: Everything you need to know”

  1. Pingback: Why C# Developers Should Love TypeScript (and Vice Versa) 🍔

  2. Pingback: String Methods in C#: Everything you need to know

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top