官术网_书友最值得收藏!

  • 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.
Compile time type is what you used to define a variable. Runtime type refers to the actual type to which a variable belongs.
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.

主站蜘蛛池模板: 遂溪县| 郓城县| 卢龙县| 凤凰县| 琼中| 神池县| 彭州市| 峨山| 沙湾县| 平泉县| 邢台县| 安塞县| 南岸区| 牡丹江市| 广宁县| 绿春县| 正安县| 中宁县| 恩平市| 千阳县| 灵武市| 西藏| 浦县| 宣城市| 洞口县| 新蔡县| 垫江县| 大英县| 德阳市| 卢龙县| 东平县| 肥乡县| 偃师市| 诸城市| 六盘水市| 靖江市| 新密市| 浏阳市| 通州市| 临沧市| 玉龙|