You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: standard/patterns.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,6 +22,28 @@ A *declaration_pattern* and a *var_pattern* can result in the declaration of a l
22
22
23
23
Each pattern form defines the set of types for input values that the pattern may be applied to. A pattern `P` is *applicable to* a type `T` if `T` is among the types whose values the pattern may match. It is a compile-time error if a pattern `P` appears in a program to match a pattern input value ([§11.1](patterns.md#111-general)) of type `T` if `P` is not applicable to `T`.
24
24
25
+
> *Example*: The following example generates a compile-time error because the compile-time type of `v` is `Stream`. A variable of type `Stream` can never be an expression of type `string`:
26
+
>
27
+
> ```csharp
28
+
>Streamv=OpenDataFile(); // compile-time type of 'v' is 'Stream'
29
+
>if (visstring) // compile-time error
30
+
> {
31
+
>/* code assuming v is a string*/
32
+
> }
33
+
> ```
34
+
>
35
+
>However, thefollowingdoesn't generate a compile-time error because the compile-time type of `v` is `object`. A variable of type `object` could be an expression of type `string`:
36
+
>
37
+
> ```csharp
38
+
>objectv=OpenDataFile();
39
+
>if (visstrings)
40
+
> {
41
+
>/* code assuming v is a string*/
42
+
> }
43
+
> ```
44
+
>
45
+
>*endexample*
46
+
25
47
Eachpatternformdefinesthesetofvaluesfor which the pattern *matches* the value at runtime.
0 commit comments