question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow by-ref Extension methods.

See original GitHub issue

Currently C# allows methods that are public and static on static classes to be extension methods by augmenting the first parameter with the this keyword.

This is all well and good, but structs cannot really benefit from this feature as much as struct method invocation is basically always by-ref. e.g. Calling 3.GetHashCode() actually translates in il like…

ldc.i3
stloc.0
ldloca.s 0
call Int32 GetHashCode

(Forgive me if that’s wrong its from memory.) The basic gist here is that we actually get a reference to the memory location and then invoke the method. My proposal is to allow a user to declare extension methods with a by ref first parameter if it is a struct. Thus:

public static void MyMethod(this ref int x){...} // allowed
public static void MyMethod<T>(this ref T x) where T:struct //allowed
public static void MyMethod(this ref string x) // not allowed.

Technically, I suppose we don’t need to restrict it. But I think the behavior isn’t as necessary for class types.

This would be a large benefit for larger structs as copying them is something you always would want to avoid.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:40
  • Comments:25 (16 by maintainers)

github_iconTop GitHub Comments

24reactions
sharwellcommented, Jan 30, 2015

The following is valid code:

public struct SomeStruct
{
    public void SomeMethod()
    {
        this = new SomeStruct();
    }
}

The following should be valid as well, with exactly the same result:

public static void SomeMethod(this ref SomeStruct value)
{
    value = new SomeStruct();
}
20reactions
thomaslevesquecommented, Feb 7, 2015

This feature would only encourage mutable structs, which are almost always a bad idea.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Doesn't C# Extension Methods allow passing parameters ...
No. In C#, you cannot specify any modifiers (like 'out' or ref ) other than this for the first parameter of an extension...
Read more >
Extension Methods - C# Programming Guide
In this article ... Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise ......
Read more >
C# Extension Method
Extension methods allow you to inject additional methods without modifying, deriving or recompiling the original class, struct or interface. Extension methods ...
Read more >
Extension Methods in the .NET Framework - Simplicity Point
NET Framework. They allow for easily extending a type without having to recompile or modify the original type. This is useful for extending...
Read more >
Extension Method in C#. Everything You Need To Learn
Extension methods allow existing classes to be extended without relying on inheritance or changing the class's source code.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found