- Learn C# in 7 days
- Gaurav Aroraa
- 463字
- 2021-07-08 09:51:24
Contextual
These are not reserved keywords, but they have a special meaning for a limited context of a program and can also be used as an identifier outside that context.
These are the contextual keywords of C#:
- add: This is used to define a custom accessor and it invokes when someone subscribes to an event. The add accessors are always followed by the remove accessors, which means when we provide the add accessor, the remove accessor should be applied thereon. For more information, refer to https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/events/how-to-implement-interface-events.
- ascending/descending: This contextual keyword is used with an orderby clause in a LINQ statement. We will discuss this in more detail on day six.
- async: This is used for an asynchronous method, lambda expression, or anonymous method. To get the result from asynchronous methods, the await keyword is used. We will be discussing this in more detail on day six.
- dynamic: This helps us bypass the compile-time type checking. This resolves types at runtime.
Let's look at the following code in order to understand these terms better:
internal class Parent
{
//stuff goes here
}
internal class Child : Parent
{
//stuff goes here
}
We can create an object of our child class like this:
Parent myObject = new Child();
Here, compile-time type for myObject is Parent as the compiler knows the variable is a type of Parent without caring or knowing about the fact that we are instantiate this object with type Child. Hence this is a compile-time type. Runtime type is the actual type that is Child in our example. Hence, runtime type of our variable myObject is Child.
Take a look at the following code snippet:
private static void DynamicTypeExample() { dynamic dynamicInt = 10; dynamic dynamicString = "This is a string"; object obj = 10; WriteLine($"Run-time type of {nameof(dynamicInt)} is {dynamicInt.GetType()}"); WriteLine($"Run-time type of {nameof(dynamicString)} is {dynamicString.GetType()}"); WriteLine($"Run-time type of {nameof(obj)} is {obj.GetType()}"); }
The above code produces following output:

For more information, refer: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/dynamic.
These are the contextual keywords that are used in query expressions; let's discuss these keywords in detail:
- from: This uses the in query expression and will be discussed on day six.
- get: This defines the accessor and is used along with properties for the retrieval of values. We will be discussing this in more detail on day six.
- group: This is used with a query expression and returns a sequence of IGroupong<Tkey,TElement> objects. We will discuss this in more detail on day six.
- into: This identifier helps store temporary data while working with query expressions. We will discuss this in more detail on day six.
For more information on contextual keywords, refer to https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords.