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

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.

主站蜘蛛池模板: 浮山县| 宜阳县| 大宁县| 绥阳县| 南澳县| 堆龙德庆县| 延长县| 黄石市| 松阳县| 盐池县| 东兴市| 永济市| 沅江市| 濮阳市| 察哈| 洪雅县| 秦皇岛市| 玉门市| 彰武县| 麻江县| 鄯善县| 车致| 舒兰市| 永泰县| 德江县| 佛坪县| 库车县| 德化县| 寻乌县| 衡阳县| 河池市| 海口市| 勐海县| 巧家县| 泸溪县| 连州市| 石嘴山市| 芦山县| 金门县| 曲沃县| 景泰县|