diff --git a/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Duplicate.cs b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Duplicate.cs new file mode 100644 index 0000000000000..e2f58db09c929 --- /dev/null +++ b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Duplicate.cs @@ -0,0 +1,14 @@ +public struct Duplicate : IGetDuplicated +{ + public int _num; + + public Duplicate(int num) + { + _num = num; + } + + public static Duplicate operator ++(Duplicate other) + => other with { _num = other._num + other._num }; + + public override string ToString() => _num.ToString(); +} diff --git a/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/GetDuplicated.cs b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/GetDuplicated.cs new file mode 100644 index 0000000000000..b0228691c04df --- /dev/null +++ b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/GetDuplicated.cs @@ -0,0 +1,4 @@ +public interface IGetDuplicated where T : IGetDuplicated +{ + static virtual T operator ++(T other) => throw new NotImplementedException(); +} diff --git a/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Program.cs b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Program.cs index 1be6412c1176b..f355e061d9676 100644 --- a/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Program.cs +++ b/docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Program.cs @@ -9,6 +9,12 @@ Console.WriteLine(str++); // +// +var num = new Duplicate(2); +num++; +Console.WriteLine(num.ToString()); +// + // var pt = new Point(3, 4); diff --git a/docs/csharp/whats-new/tutorials/static-virtual-interface-members.md b/docs/csharp/whats-new/tutorials/static-virtual-interface-members.md index bcc76592bdf81..252a9adcab4ea 100644 --- a/docs/csharp/whats-new/tutorials/static-virtual-interface-members.md +++ b/docs/csharp/whats-new/tutorials/static-virtual-interface-members.md @@ -66,6 +66,26 @@ AAAAAAAAAA This small example demonstrates the motivation for this feature. You can use natural syntax for operators, constant values, and other static operations. You can explore these techniques when you create multiple types that rely on static members, including overloaded operators. Define the interfaces that match your types' capabilities and then declare those types' support for the new interface. +## Static virtual interface methods + +Similar can be achieved using the static virtual methods of an interface. This is done with the syntax of `static` and `virtual` modifiers added to any member that should be implemented in the type implementing that interface. The following example defines the `IGetDuplicated` interface, providing any type with a constraint that a type will implement the `operator ++`: + +:::code language="csharp" source="./snippets/staticinterfaces/GetDuplicated.cs"::: + +Because virtual methods aren't abstract, they must declare their body. However, for now it's throwing the `NotImplementedException` exception by default, to highlight the lack of implementation. + +The next snippet creates a structure that implements the `IGetDuplicated` interface. This `Duplicate` structure uses the `++` operator implementation to duplicate the initial number given when creating the structure: + +:::code language="csharp" source="./snippets/staticinterfaces/Duplicate.cs"::: + +You can use this `Duplicate` type by calling the `operator ++`: + +:::code language="csharp" source="./snippets/staticinterfaces/Program.cs" id="TestDuplication"::: + +which will print the result of `4`. + +Note that without having the `operator ++` implemented, calling `num++` would still not compile, leading to [CS0023](../../misc/cs0023.md). Reason for that is that even if we have the default implementation in the static virtual method of an interface, this is still only an interface and the type implementing that interface has no such operator implemented. + ## Generic math The motivating scenario for allowing static methods, including operators, in interfaces is to support [generic math](../../../standard/generics/math.md) algorithms. The .NET 7 base class library contains interface definitions for many arithmetic operators, and derived interfaces that combine many arithmetic operators in an `INumber` interface. Let's apply those types to build a `Point` record that can use any numeric type for `T`. The point can be moved by some `XOffset` and `YOffset` using the `+` operator.