There is no info here about the new feature regarding ref extension methods for structs
⚠Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
According to my tests, this feature is available in C# 7.0 (It's not mentioned in that article either, so scheduling this for October).
@BillWagner When I compile the following code with <LangVersion>7.0</LangVersion>:
```c#
struct S {}
static class Extensions
{
static void M(ref this S s) {}
}
```
I get the error:
error CS8107: Feature 'ref extension methods' is not available in C# 7.0. Please use language version 7.2 or greater.
So I don't think it's a C# 7.0 feature.
@svick Thanks for checking. I tried the same thing on a preview build and it worked. I'll check again.
Update: I had the prototype as follows (which compiled in 7.0):
static void M(this ref S s) {}
@BillWagner, it compiles in 7.0 if you change the order of the method parameter modifiers. But if you want to consume the extension method you get a compile error:
error CS8107: Feature 'ref extension methods' is not available in C# 7.0. Please use language version 7.2 or greater.
So, the only way to use the extension method is as a static helper method:
var s = new S();
Extensions.M(ref s);
I thought this odd behavior was worth reporting to Roslyn, so I did so: https://github.com/dotnet/roslyn/issues/38486.
Most helpful comment
@BillWagner, it compiles in 7.0 if you change the order of the method parameter modifiers. But if you want to consume the extension method you get a compile error:
So, the only way to use the extension method is as a static helper method: