diff options
| author | Yong He <yonghe@outlook.com> | 2024-03-04 21:57:50 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-04 21:57:50 -0800 |
| commit | bb017e6aa8ddbfac6b9a78a66f4706964fbeaff4 (patch) | |
| tree | e902a0c319a326a035f815e6ac16a3d19fc860d3 /docs/user-guide/06-interfaces-generics.md | |
| parent | 2297623aad4c249bccae3fe363ada31e308131ac (diff) | |
Extend `as` and `is` operator to work on generic types. (#3672)
Diffstat (limited to 'docs/user-guide/06-interfaces-generics.md')
| -rw-r--r-- | docs/user-guide/06-interfaces-generics.md | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/docs/user-guide/06-interfaces-generics.md b/docs/user-guide/06-interfaces-generics.md index 59a0352fa..2436dab07 100644 --- a/docs/user-guide/06-interfaces-generics.md +++ b/docs/user-guide/06-interfaces-generics.md @@ -671,6 +671,23 @@ void main() // "success" ``` +In addition to casting from an interface type to a concrete type, `as` and `is` operator can be used on generic types as well to cast a generic type into a concrete type. For example: +```csharp +T compute<T>(T a1, T a2) +{ + if (a1 is float) + { + return reinterpret<T>((a1 as float).value + (a2 as float).value); + } + else if (T is int) + { + return reinterpret<T>((a1 as int).value - (a2 as int).value); + } + return T(); +} +// compute(1.0f, 2.0f) == 3.0f +// compute(3, 1) == 2 +``` Extensions to Interfaces ----------------------------- |
