Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
Console.WriteLine(str++);
// </TestRepeat>

// <TestUniqueInstance>
var instance = UniqueInstance.Instance;

Check failure on line 13 in docs/csharp/whats-new/tutorials/snippets/staticinterfaces/Program.cs

View workflow job for this annotation

GitHub Actions / snippets-build

D:\a\docs\docs\docs\csharp\whats-new\tutorials\snippets\staticinterfaces\Program.cs(13,31): error CS0117: 'UniqueInstance' does not contain a definition for 'Instance' [D:\a\docs\docs\docs\csharp\whats-new\tutorials\snippets\staticinterfaces\staticinterfaces.csproj]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this to work, you'll need to use the interface type:

Suggested change
var instance = UniqueInstance.Instance;
var instance = (UniqueInstance as ISingleton)?.Instance;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner I committed this suggestion, although locally, with VS still on version 17.4 I do have issues with your suggestion: CS0119 for UniqueInstance and CS0305 for ISingleton (which can be fixed by specifying the T type for this cast).
Let's see if remotely it will also fail and then we can check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BillWagner I should mention, that locally, with VS v17.4 I can also see the initial error for Instance not being a part of UniqueInstance.
Somewhere I read that static virtual feature requires:

  • .NET v8.0 or above - locally I'm running on .NET 9, and I see this staticinterfaces project runs on .NET 8, which should be fine?
  • preview enabled as LangVersion - we have that.
  • VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @BartoszKlonowski

I see the new errors. To answer your question:

.NET v8.0 or above - locally I'm running on .NET 9, and I see this staticinterfaces project runs on .NET 8, which should be fine?

Yes, it should be. As a normal practice, we're happy to move to the latest GA release (.NET 9 now).

preview enabled as LangVersion - we have that.

Check.

VS 17.6 or above - this is what I'm missing locally, but can't say regarding the CI checks.

We should be using the .NET 10 preview build (because some of the docs are for the preview).

Now, this example might be tricky to get right unless the type overrides the interface method (even if it just calls the interface implementation.) Why? Well, an ISingleton<int> could be implemented by more than one class type. So, which one should be used? Without an instance of the type, that's hard to express.

instance.PrintMessage();
// </TestUniqueInstance>

// <TestAddition>
var pt = new Point<int>(3, 4);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public interface ISingleton<T> where T : new()
{
public static virtual T Instance
{
get
{
field ??= new T();
return field;
}
private set
{
field = value;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class UniqueInstance : ISingleton<UniqueInstance>
{
public void PrintMessage()
{
Console.WriteLine("This is a unique instance");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>preview</LangVersion>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ 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

You can also provide a default implementation for static methods defined in 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 `ISingleton<T>` interface, providing any type with a constraint that a type will implement the unique `Instance` field:

:::code language="csharp" source="./snippets/staticinterfaces/Singleton.cs":::

Because virtual methods aren't abstract, they must declare their body. In this case the body is the implementation of `get` and `set` of the unique `Instance` that a singleton will share.

The next snippet creates a class that implements the `ISingleton<T>` interface. This `UniqueInstance` class does not override the behavior of `PrintMessage()` method, but relies on it's original definition:

:::code language="csharp" source="./snippets/staticinterfaces/UniqueInstance.cs":::

You can use this `UniqueInstance` type by calling the `PrintMessage()` method:

:::code language="csharp" source="./snippets/staticinterfaces/Program.cs" id="TestUniqueInstance":::

which will print the message defined in the `ISingleton` interface.

## 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<T>` interface. Let's apply those types to build a `Point<T>` record that can use any numeric type for `T`. The point can be moved by some `XOffset` and `YOffset` using the `+` operator.
Expand Down
Loading