From 662ef3ed90cd96435c05096c50f3486a1a6e9d18 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 4 May 2023 13:44:00 -0700 Subject: Update user-guide. (#2868) Co-authored-by: Yong He --- docs/user-guide/04-interfaces-generics.md | 44 +++++++++++++++---------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'docs/user-guide/04-interfaces-generics.md') diff --git a/docs/user-guide/04-interfaces-generics.md b/docs/user-guide/04-interfaces-generics.md index 322134d9f..94a08714e 100644 --- a/docs/user-guide/04-interfaces-generics.md +++ b/docs/user-guide/04-interfaces-generics.md @@ -136,25 +136,6 @@ void f() } ``` -### `This` Type - -You may use a special keyword `This` in interface definitions to refer to the type that is conforming to the interface. The following examples demonstrate a use of `This` type: -```csharp -interface IComparable -{ - int comparesTo(This other); -} -struct MyObject : IComparable -{ - int val; - int comparesTo(MyObject other) - { - return val < other.val ? -1 : 1; - } -} -``` -In this example, the `IComparable` interface declares that any conforming type must provide a `comparesTo` method that performs a comparison between an object to another object of the same type. The `MyObject` type satisfies this requirement by providing a `comparesTo` method that accepts a `MyObject` typed argument, since in the scope of `MyObject`, `This` type is equivalent to `MyObject`. - ### Static Constants You can define static constant requirements in an interface. The constants can be accessed in places where a compile-time constant is needed. @@ -175,6 +156,25 @@ struct GetValuePlus1 static const int result = GetValuePlus1.value; // result == 3 ``` +### `This` Type + +You may use a special keyword `This` in interface definitions to refer to the type that is conforming to the interface. The following examples demonstrate a use of `This` type: +```csharp +interface IComparable +{ + int comparesTo(This other); +} +struct MyObject : IComparable +{ + int val; + int comparesTo(MyObject other) + { + return val < other.val ? -1 : 1; + } +} +``` +In this example, the `IComparable` interface declares that any conforming type must provide a `comparesTo` method that performs a comparison between an object to another object of the same type. The `MyObject` type satisfies this requirement by providing a `comparesTo` method that accepts a `MyObject` typed argument, since in the scope of `MyObject`, `This` type is equivalent to `MyObject`. + ### Initializers Consider a generic method that wants to create and initialize a new instance of generic type `T`: @@ -459,10 +459,10 @@ In this example, the `Array` type has a generic type parameter, `T`, that is use Note that the builtin `vector` type also has an generic value parameter `N`. > #### Note #### -> The only type of generic value parameters are integer values. `bool`, `float` and +> The only type of generic value parameters are `int`, `uint` and `bool`. `float` and > other types cannot be used in a generic value parameter. Computations in a type -> expression are supported as long as they can be folded at compile time. For example, -`vector` is equivalent to `vector`. +> expression are supported as long as they can be evaluated at compile time. For example, +`vector` is allowed and considered equivalent to `vector`. Interface-typed Values -- cgit v1.2.3