Docs: No info about ref extension methods

Created on 16 Aug 2019  Â·  5Comments  Â·  Source: dotnet/docs

There is no info here about the new feature regarding ref extension methods for structs


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - C# What's New P1 doc-enhancement

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:

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);

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Eilon picture Eilon  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments

FrancescoBonizzi picture FrancescoBonizzi  Â·  3Comments

gmatv picture gmatv  Â·  3Comments

sebagomez picture sebagomez  Â·  3Comments