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

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.

主站蜘蛛池模板: 白城市| 六安市| 阜平县| 罗定市| 平山县| 庆元县| 突泉县| 北京市| 隆林| 西丰县| 久治县| 堆龙德庆县| 江都市| 濮阳市| 汝州市| 万山特区| 大方县| 馆陶县| 镇雄县| 太湖县| 射洪县| 福州市| 措勤县| 河北省| 施甸县| 长治县| 金华市| 康定县| 海盐县| 樟树市| 张北县| 广德县| 惠安县| 鹰潭市| 阿克陶县| 三明市| 永春县| 靖江市| 汪清县| 铁力市| 饶河县|